问题描述
我有以下问题:
我想实现的Instagram到我的网站。但是我坚持在台阶上,我需要得到艾策斯令牌。 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,所以我发现这个相当于:
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.
我是什么,我做错了什么?
What I am i doing wrong?
推荐答案
几乎没有了Instagram的API需要一个POST而不是GET。
Almost there the instagram api expects a POST not a GET.
添加POST参数。
VAR的结果= client.UploadValues(,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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!