如何使用.NET SDK在PayPal中获取刷新令牌OAuth2

时间:2019-04-25 13:15:23

标签: paypal oauth-2.0 token refresh

我想为PayPal生成一个刷新令牌,并使用该刷新令牌生成新的访问令牌。但是,到目前为止我还没有实现任何东西,而且PayPal文档也不是那么好。



我尝试过的第一种方法:

我正在使用Paypal OAuth login example doesn't work as expected中的代码,并将其更改为VB.NET,如下所示,但出现400错误请求错误

    Dim oAuthUrl = "https://api.sandbox.paypal.com/v1/identity/openidconnect/tokenservice"
    Dim authHeader = String.Format("Basic {0}", Convert.ToBase64String(Encoding.UTF8.GetBytes(Uri.EscapeDataString(oAuthClientId) & ":" + Uri.EscapeDataString((oAuthClientSecret)))))
    Dim postBody = String.Format("grant_type=authorization_code&code={0}", code)
    System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12
    Dim authRequest = CType(WebRequest.Create(oAuthUrl), HttpWebRequest)
    authRequest.Headers.Add("Authorization", authHeader)
    authRequest.Method = "POST"
    Dim postcontentsArray As Byte() = Encoding.UTF8.GetBytes(postBody)
    authRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8"
    authRequest.ContentLength = postcontentsArray.Length

    Try

        Using stream As Stream = authRequest.GetRequestStream()
            stream.Write(postcontentsArray, 0, postcontentsArray.Length)
            stream.Close()
            Dim response As WebResponse = authRequest.GetResponse()

            Using responseStream As Stream = response.GetResponseStream()

                If responseStream IsNot Nothing Then

                    Using reader = New StreamReader(responseStream)
                        Dim responseFromServer As String = reader.ReadToEnd()
                        reader.Close()
                        responseStream.Close()
                        response.Close()
                        Dim responseResult = JsonConvert.DeserializeObject(responseFromServer)
                    End Using
                End If
            End Using
        End Using

    Catch e As Exception
    End Try




我尝试过的第二种方法:

我还尝试通过PayPal .NET SDK使用CreateFromAuthorizationCodeParameters,但得到的响应是客户端ID或机密为空:

    Dim config As Dictionary(Of String, String) = generateConfig()
    Dim accessToken = New PayPal.OAuthTokenCredential(sClientID, sClientSecret, config).GetAccessToken()
    Dim apiContext = New PayPal.APIContext(accessToken)
    apiContext.Config = config


    Dim createFromAuthorizationCodeParameters1 As CreateFromAuthorizationCodeParameters = New CreateFromAuthorizationCodeParameters()
    createFromAuthorizationCodeParameters1.SetCode(sAuthCode)
    createFromAuthorizationCodeParameters1.setClientId(sClientID)
    createFromAuthorizationCodeParameters1.setClientSecret(sClientSecret)
    Dim Tokeninfo As Tokeninfo = Tokeninfo.CreateFromAuthorizationCode(apiContext, createFromAuthorizationCodeParameters1)
    Dim createFromRefreshTokenParameters As CreateFromRefreshTokenParameters = New CreateFromRefreshTokenParameters()
    Dim refreshedTokenInfo As Tokeninfo = Tokeninfo.CreateFromRefreshToken(createFromRefreshTokenParameters)

“ config”字典如下:

    Dim config As New Dictionary(Of String, String)()
    config.Add("mode", "live")
    config.Add("connectionTimeout", "30000")
    config.Add("requestRetries", "5")
    config.Add("IPAddress", "127.0.0.1")
    config.Add("account1.apiUsername", sUserName)
    config.Add("account1.apiPassword", sPassword)
    config.Add("account1.apiSignature", sSignature)
    config.Add("account1.applicationId", sAppID)
    config.Add("clientId", sClientID)
    config.Add("clientSecret", sClientSecret)
    config.Add("account1.signatureSubject", "")
    config.Add("account1.certificateSubject", "")

这些方法都无法生成刷新令牌。任何帮助将不胜感激。谢谢

0 个答案:

没有答案