.NET 中的 OAuth 2.0 和 Instagram API [英] OAuth 2.0 In .NET With Instagram API

查看:17
本文介绍了.NET 中的 OAuth 2.0 和 Instagram API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力使用 Instagram API,但我停留在他们 OAuth 的第 2 步.我有一个从他们重定向回我的代码,但是他们希望我用如下参数发布一个帖子......

I am working on consuming the Instagram API and I am stuck at step 2 of their OAuth. I have a code from their redirect back to me, but then they want me to do a post with parameters like below...

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 MVC 3 解决方案来实现.我试着像这样实现帖子......

I am implementing this as an ASP.NET MVC 3 solution. I tried to implement the post like so...


    WebRequest request = HttpWebRequest.Create("https://api.instagram.com/oauth/access_token");
    request.Method = "POST";
    request.Headers.Add("client_id", "sdlf0982jiejfopijfp92jjiwoijf90");
    request.Headers.Add("client_secret", "39993939393939393939393939393");
    request.Headers.Add("grant_type", "authorization_code");
    request.Headers.Add("redirect_uri", "http://localhost:34962/Home/Auth");
    request.Headers.Add("code", 111111);
    var response = request.GetResponse();
    return View();

这给了我一个 400 错误,说client_id 是必需的".我已经包含了 client_id,但我显然没有正确实现它.

This gives me a 400 error saying that "client_id is required". I have included the client_id, but I'm clearly not implementing this correctly.

执行 OAuth 第二阶段的最佳实践"方式是什么?

What is the "best practice" way to perform the second leg of the OAuth?

推荐答案

我从 上面提到的 SO post 关于将 POST 参数添加到 HttpWebRequest.这是我的实现细节.

I got the answer from the above mentioned SO post about adding POST parameters to an HttpWebRequest. Here are the details of my implementation.

NameValueCollection parameters = new NameValueCollection();
parameters.Add("client_id", "3498wjfoi2892jf0j2ij02fjakjf2");
parameters.Add("client_secret", "392621gfdlfj2k2hf7g2lfhj2g");
parameters.Add("grant_type", "authorization_code");
parameters.Add("redirect_uri", "http://localhost:34962/Home/Auth");
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);

return View("Index", (object)response);

这篇关于.NET 中的 OAuth 2.0 和 Instagram API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆