OAuth 2.0 In .NET With Instagram API

前端 未结 2 1486
醉话见心
醉话见心 2020-12-31 06:44

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 p

相关标签:
2条回答
  • 2020-12-31 07:21

    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();
        }
    
    0 讨论(0)
  • 2020-12-31 07:24

    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);
    
    0 讨论(0)
提交回复
热议问题