locked
access Token Linked In authentication user data ASP.Net LinkedIn API RRS feed

  • Question

  • User-22523062 posted

    Well After a long search in google I having to post the very old issue here , Please tell me where I am doing mistake my all code working fine till access token

    I am receiving access token successfully but the issue when I send access token to get user details 

    here is my code 

      protected void btnlogin_Click(object sender, EventArgs e)
            {
                System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
    
    
    string LinkedInurl = "https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=" + LinkiedClientID + "&scope=r_basicprofile%20r_emailaddress&state=DCEEFWF45453sdffef424&redirect_uri=" + redirection_url + "";
                
         Response.Redirect(LinkedInurl);
            }

    On another page load

          protected void Page_Load(object sender, EventArgs e)
            {
    
                System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
    
                if (!IsPostBack)
                {
                    if (Request.QueryString["code"] != null)
                    {
                        ///GetToken(Request.QueryString["code"].ToString());
                        GetLinkedToken(Request.QueryString["code"].ToString());
    
    
    
    
                    }
                }
            }
    public void GetLinkedToken(string code)
            {
    
            
    
                string authUrl = "https://www.linkedin.com/uas/oauth2/accessToken";
    
                var sign = "grant_type=authorization_code" + "&code=" + code + "&redirect_uri=" + HttpUtility.HtmlEncode(redirection_url) + "&client_id=" + LinkiedClientID + "&client_secret=" + LinkiedClientPass;
                // var postData = String.Format("grant_type=authorization_code&code={0}&redirect_uri={1}&client_id={2}&client_secret={3}", code, HttpUtility.HtmlEncode(redirectUrl), apiKey, apiSecret);
    
                HttpWebRequest webRequest = WebRequest.Create(authUrl + "?" + sign) as HttpWebRequest;
                webRequest.Method = "POST";
                webRequest.ContentType = "application/x-www-form-urlencoded";
    
                Stream dataStream = webRequest.GetRequestStream();
    
                String postData = String.Empty;
                byte[] postArray = Encoding.ASCII.GetBytes(postData);
    
                dataStream.Write(postArray, 0, postArray.Length);
                dataStream.Close();
    
                WebResponse response = webRequest.GetResponse();
                dataStream = response.GetResponseStream();
    
    
                StreamReader responseReader = new StreamReader(dataStream);
    
                string responseFromServer=   responseReader.ReadToEnd().ToString();
            
            JavaScriptSerializer js = new JavaScriptSerializer();
                Tokenclass obj = js.Deserialize<Tokenclass>(responseFromServer);
                GetLinkeduserProfile(obj.access_token);
    
                 
            }

    ---Till here code is working perfectly but in GetLinkeduserProfile(obj.access_token); I am recieving errors 

    some time The remote server returned an error: (401) Unauthorized.

    and some time 403

     public void GetLinkeduserProfile(string accesstoken)
            {
    string url = "https://api.linkedin.com/v1/people/firstName/?xoauth_oauth2_access_token=" + accesstoken + "&format=json";
    
    
    
    HttpWebRequest webRequest = WebRequest.Create(url) as HttpWebRequest;
                webRequest.Method = "POST";
                webRequest.ContentType = "application/x-www-form-urlencoded";
    
                Stream dataStream = webRequest.GetRequestStream();
    
                String postData = String.Empty;
                byte[] postArray = Encoding.ASCII.GetBytes(postData);
    
                dataStream.Write(postArray, 0, postArray.Length);
                dataStream.Close();
    
                WebResponse response = webRequest.GetResponse();
                dataStream = response.GetResponseStream();
    
    
                StreamReader responseReader = new StreamReader(dataStream);
                string responseFromServer = responseReader.ReadToEnd();
                responseReader.Close();
                response.Close();
                JavaScriptSerializer js = new JavaScriptSerializer();
                Userclass userinfo = js.Deserialize<Userclass>(responseFromServer);
                imgprofile.ImageUrl = userinfo.picture;
                lblid.Text = userinfo.id;
                lblgender.Text = userinfo.gender;
                lbllocale.Text = userinfo.locale;
                lblname.Text = userinfo.name;
                hylprofile.NavigateUrl = userinfo.link;
                hylprofile.Text = userinfo.email;
    
    
    
            }

    Please Experts , Help me and tell me where I am making mistake 

    Monday, March 9, 2020 7:17 AM

All replies

  • User-17257777 posted

    Hi SyedAzy,

    Version 1.0 of Linked In API is no longer supported since May 1, 2019. You may use v2 instead.

    The Authorization Code Flow has the following steps:

    1. Configure your application to get the Client ID and Client Secret.
    2. Your application directs the browser to LinkedIn's OAuth 2.0 authorization page where the member authenticates. After authentication, LinkedIn's authorization server passes an authorization code to your application.
    3. Your application sends this code to LinkedIn and LinkedIn returns an access token.
    4. Your application uses this token to call APIs, including an Authorization header in the HTTP call to LinkedIn's API.

    For more details, refer to the document:

    https://docs.microsoft.com/en-us/linkedin/shared/authentication/authorization-code-flow?context=linkedin/context#step-1-configure-your-application

    Best Regards,

    Jiadong Meng

    Tuesday, March 10, 2020 6:26 AM
  • User-22523062 posted

    Well the link you posted is very helpful but it resolve by simple step.

    I just changed the following request below to get the user detail and all is working fine.  

    https://api.linkedin.com/v2/people/me/?oauth2_access_token
    https://api.linkedin.com/v2/emailAddress?q=members&projection=(elements*(handle~))&oauth2_access_token=

    but in the end , I want to thanks because your diverted link was very helpful.

    Wednesday, March 18, 2020 6:07 AM