Authentication Paypal OAuth登录示例没有';I don’我没有按预期工作

Authentication Paypal OAuth登录示例没有';I don’我没有按预期工作,authentication,oauth,paypal,paypal-sandbox,Authentication,Oauth,Paypal,Paypal Sandbox,我从贝宝的网站跟踪 public ActionResult PaypalResponse(string scope, string code) { Dictionary<string, string> configurationMap = new Dictionary<string, string>(); configurationMap.Add("mode", "sandbox"); APIContext a

我从贝宝的网站跟踪

  public ActionResult PaypalResponse(string scope, string code)
    {
        Dictionary<string, string> configurationMap = new Dictionary<string, string>();
        configurationMap.Add("mode", "sandbox");

        APIContext apiContext = new APIContext();
        apiContext.Config = configurationMap;

        CreateFromAuthorizationCodeParameters param = new CreateFromAuthorizationCodeParameters();
        param.setClientId(clientId);
        param.setClientSecret(clientSecret);
        param.SetCode(code);

         // Exception here: 
        Tokeninfo info = Tokeninfo.CreateFromAuthorizationCode(apiContext, param);

        return null;
    }
小提琴手的输出是

HTTP/1.1 400 Bad Request
Server: Apache-Coyote/1.1
Cache-Control: no-store
Pragma: no-cache
Content-Type: application/json
Vary: Accept-Encoding
Content-Length: 76
DC: origin1-api.sandbox.paypal.com
Date: Mon, 14 Oct 2013 01:30:05 GMT
Connection: close
Set-Cookie: DC=origin1-api.sandbox.paypal.com; secure

{"error_description":"client id or secret is null","error":"invalid_client"}

我对PayPal也有一些问题,尽管不是这个示例。我用nodejs。工作正常,您可能希望将注释与您正在使用的注释进行比较,看看是否有任何差异。(端点当然不同)。具体请参见。

我遇到了完全相同的问题,并通过更改调用方法解决了它

string postcontents = string.Format("client_id={0}&client_secret={1}&grant_type=authorization_code&redirect_uri={2}&code={3}"
                                      , System.Web.HttpUtility.UrlEncode(clientId)
                                      , System.Web.HttpUtility.UrlEncode(secret)
                                      , System.Web.HttpUtility.UrlEncode(url)
                                      , System.Web.HttpUtility.UrlEncode(code));


        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://api.sandbox.paypal.com/v1/identity/openidconnect/tokenservice");
        request.Credentials = new NetworkCredential(clientId, secret);
        request.Method = "POST";
        byte[] postcontentsArray = System.Text.Encoding.UTF8.GetBytes(postcontents);
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = postcontentsArray.Length;
        //OAuth.
        using (Stream requestStream = request.GetRequestStream())
        {
            requestStream.Write(postcontentsArray, 0, postcontentsArray.Length);
            requestStream.Close();
            WebResponse response = request.GetResponse();
            using (Stream responseStream = response.GetResponseStream())
            using (StreamReader reader = new StreamReader(responseStream))
            {
                string responseFromServer = reader.ReadToEnd();
                reader.Close();
                responseStream.Close();
                response.Close();
                // return SerializeToken(responseFromServer);
                dynamic dynObj = JsonConvert.DeserializeObject(responseFromServer);
                string token = dynObj["access_token"];
                //token = ser.Deserialize<ImportContacts._Default.GoogleOAuthToken>(responseFromServer);
            }
        }
string postcontents=string.Format(“客户端\u id={0}&client\u secret={1}&grant\u type=authorization\u code&redirect\u uri={2}&code={3}”
,System.Web.HttpUtility.UrlEncode(clientId)
,System.Web.HttpUtility.UrlEncode(机密)
,System.Web.HttpUtility.UrlEncode(url)
,System.Web.HttpUtility.UrlEncode(code));
HttpWebRequest请求=(HttpWebRequest)HttpWebRequest.Create(“https://api.sandbox.paypal.com/v1/identity/openidconnect/tokenservice");
request.Credentials=新的网络凭据(clientId,secret);
request.Method=“POST”;
byte[]postcontentsArray=System.Text.Encoding.UTF8.GetBytes(postcontents);
request.ContentType=“application/x-www-form-urlencoded”;
request.ContentLength=postcontentsArray.Length;
//非统组织。
使用(Stream requestStream=request.GetRequestStream())
{
Write(postcontentsArray,0,postcontentsArray.Length);
requestStream.Close();
WebResponse=request.GetResponse();
使用(Stream responseStream=response.GetResponseStream())
使用(StreamReader=新StreamReader(responseStream))
{
字符串responseFromServer=reader.ReadToEnd();
reader.Close();
responseStream.Close();
response.Close();
//返回序列化令牌(responseFromServer);
动态dynObj=JsonConvert.DeserializeObject(responseFromServer);
字符串令牌=dynObj[“访问令牌”];
//令牌=服务反序列化(responseFromServer);
}
}

希望它有帮助

根据@pollirrata answer更新,这就是它对我的作用。希望它能帮助别人

    //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
        }
    }
在此之后,您可以使用新令牌调用GET方法以获取用户信息,uri:


请参阅:

我在这里遇到了相同的问题,我正在尝试您的方法。然而,在返回到我的站点之后,在

WebResponse response = authRequest.GetResponse();
我已经验证了我的clientId和secret是否正确,以及是否已成功地将authCode返回给该方法。你可能也遇到过这个问题吗

谢谢

---更新---


清理了解决方案和浏览器缓存,现在看起来确实正常了

Sailen上面的回答对我很有用。但是,由于Paypal在2016年6月将加密更改为TLS,因此编写的解决方案将返回以下错误

请求被中止:无法创建SSL/TLS安全通道

根据另一篇文章中提供的答案,在创建
WebRequest
之前,需要将
SecurityProtocol
设置为
Tls12。希望这对别人有帮助

这里有一个片段

var postBody = $"grant_type=authorization_code&code={code}";

System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;

var authRequest = (HttpWebRequest)WebRequest.Create(oAuthUrl);
var postBody = $"grant_type=authorization_code&code={code}";

System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;

var authRequest = (HttpWebRequest)WebRequest.Create(oAuthUrl);