本文介绍了请求 access_token Instagram的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了以下问题:

我正在尝试在我的网站中实施 Instagram.但是,我被困在需要获取 Access 令牌的步骤上.api 的文档说我需要像这样请求它:

I am trying to implement Instagram into my website. However I am stuck on the step where I need to get the Acces token. The api's documentation says I need to request it like this :

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 在.NET 和 Instagram API:

I use ASP.NET so I found this equivalent 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) 错误请求.

However I keep getting: System.Net.WebException: The remote server returned an error: (400) Bad Request.

我做错了什么?

推荐答案

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

Almost there the instagram api expects a POST not a GET.

添加POST"参数.

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

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

还要检查 instagram 设置 -> 重定向 url.

Also check the instagram settings -> redirect url.

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

Then this may help don't forget to add a reference to Newtonsoft.Json. Is in .Net version 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;
                }
            }
        }
    }
}

这篇关于请求 access_token Instagram的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 14:24