Tuesday 19 July 2016

PINTEREST GET OAUTH CODE USING C#

public string GetOAuthToken(string usrcode)
        {
            string strResult = string.Empty;
            try
            {            
                    string Clientid = WebConfigurationManager.AppSettings["Pinterest_Clientid"];
                    string ClientSecret = WebConfigurationManager.AppSettings["Pinterest_ClientSecret"];
                    string uri_token = WebConfigurationManager.AppSettings["Pinterest_Uri_Token"];
                    System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri_token);

                    string parameters = "grant_type=authorization_code"
                                        + "&client_id="
                                        + Clientid
                                        + "&client_secret="
                                        + ClientSecret
                                        + "&code="
                                        + usrcode;

                    req.ContentType = "application/x-www-form-urlencoded";
                    req.Method = "POST";
                    byte[] bytes = Encoding.ASCII.GetBytes(parameters);
                    System.IO.Stream os = null;
                    req.ContentLength = bytes.Length;
                    os = req.GetRequestStream();
                    os.Write(bytes, 0, bytes.Length);
                    System.Net.WebResponse webResponse = req.GetResponse();
                    System.IO.Stream stream = webResponse.GetResponseStream();
                    System.IO.StreamReader reader = new System.IO.StreamReader(stream);
                    string response = reader.ReadToEnd();
                    Newtonsoft.Json.Linq.JObject o = Newtonsoft.Json.Linq.JObject.Parse(response);
                    strResult = "SUCCESS:" + o["access_token"].ToString();
            }
            catch (Exception ex)
            {
                strResult = "ERROR:" + ex.Message.ToString();
            }
            return strResult;
        }

6 comments:

  1. Hey Mohan, how do we get the authorization code and wat is this usrcode you passed to the method? Please post the complete code if u have.

    Thanks
    Ram

    ReplyDelete
    Replies
    1. Hi Ram,

      window.location = 'https://api.pinterest.com/oauth/?' +
      'response_type=code&' +
      'redirect_uri='your_return_site_page' +
      'client_id=your_pinterest_client_id&' +
      'scope=read_public,write_public&' +
      'state=768uyFys'

      By Using This After User Login Pinterest return the User Authorize Code.

      Delete
  2. Hey Mohan, how do we get the authorization code and wat is this usrcode you passed to the method? Please post the complete code if u have.

    Thanks
    Ram

    ReplyDelete
    Replies
    1. Hi Ram,

      window.location = 'https://api.pinterest.com/oauth/?' +
      'response_type=code&' +
      'redirect_uri='your_return_site_page' +
      'client_id=your_pinterest_client_id&' +
      'scope=read_public,write_public&' +
      'state=768uyFys'

      By Using This After User Login Pinterest return the User Authorize Code.

      Delete
  3. This comment has been removed by the author.

    ReplyDelete
  4. What is uri_token & how to get it? I only got client_id & client_secret when i created an app in pinterest.

    ReplyDelete