微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 请求access_token Instagram

我遇到了以下问题:

我正在尝试将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;
                }
            }
        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐