OAuth 2.0用户在.NET中使用Instagram的API [英] OAuth 2.0 In .NET With Instagram API

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

问题描述

我的工作消耗了Instagram的API和我停留在他们的OAuth的第2步。我从他们的重定向code还给我,但后来他们要我做的一个帖子中包含...

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参数的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);

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

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