2

我正在关注Paypal 网站上的这个交互式登录示例

  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;
    }

CreateFromAuthorizationCode 方法会导致此(不正确的)调用。请注意缺少客户端 ID 和客户端密码

POST https://api.sandbox.paypal.com/v1/identity/openidconnect/tokenservice HTTP/1.1
Content-Type: application/x-www-form-urlencoded
User-Agent: PayPalSDK/  ;lang=DOTNET;v=4.0.30319.18051;bit=64;os=Microsoft Windows 8 Enterprise 6.2.9200.0;
Host: api.sandbox.paypal.com
Content-Length: 211
Expect: 100-continue
Connection: Keep-Alive

grant_type=authorization_code&code=d2mSEzm9xvE_l9Ibho0g6FNBVrC7wHZchJWqJfY...redacted...

提琴手输出是

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"}
4

5 回答 5

1

我有完全相同的问题,并解决了它改变调用方法

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);
            }
        }

希望能帮助到你

于 2014-02-12T19:33:30.580 回答
0

我在这里遇到同样的问题,我正在尝试你的方法。但是,在返回我的网站后,我收到了 400 错误请求,在

WebResponse response = authRequest.GetResponse();

我已经验证了我的 clientId 和 secret 是正确的,并且 authCode 已成功返回到该方法。您是否也遇到过这个问题?

谢谢

- - 更新 - -

清理了解决方案和浏览器缓存,现在似乎可以正常工作

于 2015-02-17T01:51:11.563 回答
0

根据@pollirrata 答案更新,这就是它对我的工作方式。希望它会帮助某人。

    //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:https ://api.sandbox.paypal.com/v1/identity/openidconnect/userinfo/?schema=openid

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

于 2014-05-15T06:46:55.593 回答
0

我对 PayPal 也有一些问题,尽管这个示例没有。我使用了nodejs。这个库工作正常,您可能想将笔记与您正在使用的库进行比较,看看您是否发现任何不同之处。(端点肯定不同)。具体看这里

于 2013-10-15T05:25:54.440 回答
0

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

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

根据另一个线程中提供的答案,SecurityProtocol需要设置为Tls12 before创建WebRequest. 希望这可以帮助某人。

这是一个片段。

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

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

var authRequest = (HttpWebRequest)WebRequest.Create(oAuthUrl);
于 2017-09-24T20:33:49.687 回答