14

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

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();

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.

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

Burke Holland
  • 2,325
  • 1
  • 22
  • 33
  • I found this related SO question which I think is what I'm looking for. http://stackoverflow.com/questions/3279888/how-to-add-parameters-into-a-webrequest – Burke Holland Aug 23 '11 at 14:16
  • If you prefer HttpWebRequest to WebClient, see the answer [here](http://stackoverflow.com/questions/25094609/request-the-access-token-instagram/37593946#37593946) – Victor Stagurov Jun 02 '16 at 14:51

2 Answers2

18

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);
Community
  • 1
  • 1
Burke Holland
  • 2,325
  • 1
  • 22
  • 33
  • I want to use the `WebClient UploadData()` method to upload binary data. I already have an access token for my single page application,; can you tell me how to use the token in the `UploadData()` method as it does not take a `NameValueCollection` as a parameter. – toing_toing Dec 29 '15 at 05:40
4

I spent a lot of time on my task because i didn't see response error.

        try
    {
        NameValueCollection parameters = new NameValueCollection();
        parameters.Add("client_id", "638ed32066b04801bd40aa05c1542915");
        parameters.Add("client_secret", "fc67cf3645a648ce82106298010acd65");
        parameters.Add("grant_type", "authorization_code");
        parameters.Add("redirect_uri", "http://localhost:34962/Test/InstagramCallback");
        parameters.Add("code", code);

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

        return Encoding.Default.GetString(result);
    }
    catch (WebException ex)
    {
        StreamReader reader = new StreamReader(ex.Response.GetResponseStream());
        string line;
        StringBuilder result = new StringBuilder();
        while ((line = reader.ReadLine()) != null)
        {
            result.Append(line);
        }
        return result.ToString();
    }
Oleksandr Fentsyk
  • 5,256
  • 5
  • 34
  • 41
  • Can I use it in my *Windows Forms app* ? Maybe problem with _redirect_uri_ ? ***Cliend_id*** and ***Client_secret*** I can get in api.instagram page. – Kiquenet Jul 09 '16 at 11:47