加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 请求access_token Instagram

发布时间:2020-12-15 22:06:35 所属栏目:百科 来源:网络整理
导读:我遇到了以下问题: 我正在尝试将Instagram应用到我的网站中.但是我仍然坚持我需要获取Acces令牌的步骤. api的文档说我需要像这样请求它: curl -F 'client_id=CLIENT-ID' -F 'client_secret=CLIENT-SECRET' -F 'grant_type=authorization_code' -F 'red
我遇到了以下问题:

我正在尝试将Instagram应用到我的网站中.但是我仍然坚持我需要获取Acces令牌的步骤. api的文档说我需要像这样请求它:

curl -F 'client_id=CLIENT-ID' 
-F 'client_secret=CLIENT-SECRET' 
-F 'grant_type=authorization_code' 
-F 'redirect_uri=YOUR-REDIRECT-URI' 
-F 'code=CODE' https://api.instagram.com/oauth/access_token

我使用ASP.NET所以我发现这相当于OAuth 2.0 In .NET With Instagram API:

NameValueCollection parameters = new NameValueCollection();
            parameters.Add("client_id","ssdfsdfsdfsdfsdf");
            parameters.Add("client_secret","sdsdfdsfsdfsdfsdfsdfsdf");
            parameters.Add("grant_type","authorization_code");
            parameters.Add("redirect_uri","http://localhost:2422/LoginsGuests/GetLoginPage");
            parameters.Add("code",code);

            WebClient client = new WebClient();
            var result = client.UploadValues("https://api.instagram.com/oauth/access_token",parameters);

            var response = System.Text.Encoding.Default.GetString(result);

但是我一直在:
????System.Net.WebException:远程服务器返回错误:(400)错误请求.

我做错了什么?

解决方法

几乎在那里,instagram api期望POST不是GET.

添加“POST”参数.

var result = client.UploadValues(“https://api.instagram.com/oauth/access_token”,“POST”,参数);

另请检查Instagram设置 – >重定向网址.

然后这可能有助于不要忘记添加对Newtonsoft.Json的引用.在.Net版本4.5.1中:

using System;
using System.Collections.Specialized;
using System.Net;
using System.Text;

namespace Instagram
{
    public class InstagramClient
    {
        public InstagramClient(string code)
        {
            GetToken(code);
        }

        private void GetToken(string code)
        {
            using (var wb = new WebClient())
            {
                var parameters = new NameValueCollection
                                 {
                                     {"client_id","ClientId"},{"client_secret","ClientSecret"},{"grant_type","authorization_code"},{"redirect_uri","RedirectUri"},{"code",code}
                                 };

                var response = wb.UploadValues("https://api.instagram.com/oauth/access_token","POST",parameters);
                string json = Encoding.ASCII.GetString(response);

                try
                {
                    var OauthResponse = (InstagramOAuthResponse)    Newtonsoft.Json.JsonConvert.DeserializeObject(json,typeof(InstagramOAuthResponse));
                }
                catch (Exception ex)
                {
                    //handle ex if needed.
                }
            }
        }

        public class InstagramOAuthResponse
        {
            public string access_token { get; set; }
            public User user { get; set; }
        }

        public class User : System.Security.Principal.IIdentity
        {
            public string username { get; set; }
            public string website { get; set; }
            public string profile_picture { get; set; }
            public string full_name { get; set; }
            public string bio { get; set; }
            public string id { get; set; }

            public string OAuthToken { get; set; }

            public string AuthenticationType
            {
                get { return "Instagram"; }
            }

            public bool IsAuthenticated
            {
                get { return !string.IsNullOrEmpty(id); }
            }

            public string Name
            {
                get
                {
                    return String.IsNullOrEmpty(full_name) ? "unknown" : full_name;
                }
            }
        }
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读