我想在网站和应用上显示各种Instagram用户的Feed . 内容是公开的 . 我打算使用Instagram api endpoints 来检索数据 . 要访问Instagram API,我需要一个访问令牌 . 但是我无法通过API调用获得有效的访问令牌 . 我想使用oauth 2隐式流(客户端凭证),因为交互是静默的,不应涉及手动授权(用户不必授权访问) . 以下C#代码给了我一个“BAD REQUEST”响应 . 客户端ID,密码和重定向URL已在Instagram客户端配置中设置 .
string instagramClientId = "1111111111111111111111111111111";
string instagramClientSecret = "2222222222222222222222222222222";
string instagramTokenUrl = "https://api.instagram.com/oauth/access_token";
string instagramRedirectUrl = "http://socialmedia.local/api/posts/instagram";
string instagramAccessToken = "";
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(instagramTokenUrl);
client.DefaultRequestHeaders.Accept.Clear();
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("client_id", instagramClientId),
new KeyValuePair<string, string>("client_secret", instagramClientSecret),
new KeyValuePair<string, string>("grant_type", "authorization_code"),
new KeyValuePair<string, string>("redirect_uri", instagramRedirectUrl),
new KeyValuePair<string, string>("code", "CODE")
});
HttpResponseMessage response = await client.PostAsync("", content);
if (response.IsSuccessStatusCode)
{
var result = response.Content.ReadAsStringAsync().Result;
if (result.IndexOf("access_token") >= 0)
{
instagramAccessToken = result.Substring(result.IndexOf("=") + 1);
}
}
}
1 回答
如果您发出未经授权的请求(即没有用户访问令牌),则不需要oauth2隐式流 . 您只需将
access_token:{users access token}
替换为client_id:{applications client id}
请注意,您只能对某些 endpoints 执行此操作 . 例如,您无法获得用户Feed(他们关注的人发布的最新帖子),因为这是用户私有的,需要访问令牌 . 您可以从用户那里获得最近的帖子