Paypal OAuth login example doesn't work as expected

前端 未结 5 1988
不思量自难忘°
不思量自难忘° 2021-01-15 05:06

I\'m following this interactive login example from Paypal\'s site.

  public ActionResult PaypalResponse(string scope, string code)
    {
        Dictionary&l         


        
5条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-15 05:36

    updated based on @pollirrata answer, and this is how it worked for me. Hope it will help someone.

        //code is what you get from LoginWithPayPal login page in return (query string)
    public ....  GetRefreshAccessToken(string code)
        {            
            var oAuthClientId = "clientid from paypal developer site";
            var oAuthClientSecret = "client secret from paypal developer site";
            var oAuthUrl = "https://api.sandbox.paypal.com/v1/identity/openidconnect/tokenservice";
    
            var authHeader = string.Format("Basic {0}",
                                           Convert.ToBase64String(
                                               Encoding.UTF8.GetBytes(Uri.EscapeDataString(oAuthClientId) + ":" +
                                                                      Uri.EscapeDataString((oAuthClientSecret)))
                                               ));
    
            //passing code here
            var postBody = string.Format("grant_type=authorization_code&code={0}", code);
    
            var authRequest = (HttpWebRequest)WebRequest.Create(oAuthUrl);
            authRequest.Headers.Add("Authorization", authHeader);
            authRequest.Method = "POST";
            byte[] postcontentsArray = Encoding.UTF8.GetBytes(postBody);
            authRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
            authRequest.ContentLength = postcontentsArray.Length;
    
            try
            {
                using (Stream stream = authRequest.GetRequestStream())
                {
                    stream.Write(postcontentsArray, 0, postcontentsArray.Length);
                    stream.Close();
    
                    WebResponse response = authRequest.GetResponse();
                    using (Stream responseStream = response.GetResponseStream())
                        if (responseStream != null)
                        {
                            using (var reader = new StreamReader(responseStream))
                            {
                                string responseFromServer = reader.ReadToEnd();
                                reader.Close();
                                responseStream.Close();
                                response.Close();
                                //this will return you access token which you can use to get user information
                                var responseResult =
                                    JsonConvert.DeserializeObject(responseFromServer);                                
                            }
                        }
                }
            }
            catch (Exception e)
            {
                //log error
            }
        }
    

    After this you can call GET method with new token for user information, uri: https://api.sandbox.paypal.com/v1/identity/openidconnect/userinfo/?schema=openid

    Refer to: https://developer.paypal.com/webapps/developer/docs/integration/direct/identity/log-in-with-paypal/

提交回复
热议问题