如何使用REST请求获取Paypal访问令牌 [英] How to get a paypal access token using REST request

查看:416
本文介绍了如何使用REST请求获取Paypal访问令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使其正常运行 https://developer.paypal.com/webapps/developer/docs/integration/direct/make-your-first-call/ 使用Java + Jersey应用程序.似乎我在POST参数中缺少了一些东西.

I'm trying to get this working https://developer.paypal.com/webapps/developer/docs/integration/direct/make-your-first-call/ Using Java + Jersey application. It seems like I'm missing something in POST params.

public String getPaypalToken() {
    Client client = Client.create();
    WebResource webResource = client.resource("https://api.sandbox.paypal.com/v1/oauth2/token");
    MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl();
    queryParams.add("username", CLIENT_ID + ":" + SECRET );
    queryParams.add("grant_type", "client_credentials");
    ClientResponse response = webResource.accept("application/json").acceptLanguage("en_US").type("application/x-www-form-urlencoded").post(ClientResponse.class, queryParams);
    return response.toString();
}

使用我之前得到的代码:POST https://api.sandbox.paypal.com /v1/oauth2/token 返回的响应状态为401未经授权.

Using previous code I got: POST https://api.sandbox.paypal.com/v1/oauth2/token returned a response status of 401 Unauthorized.

此CURL命令行选项可以正常工作:

This CURL command line option just works fine:

curl -X POST https://api.sandbox.paypal.com/v1/oauth2/token  -H "Accept: application/json"  -H "Accept-Language: en_US"  -u "EOJ2S-Z6OoN_le_KS1d75wsZ6y0SFdVsY9183IvxFyZp:EClusMEUk8e9ihI7ZdVLF5cZ6y0SFdVsY9183IvxFyZp"  -d "grant_type=client_credentials"

任何建议将不胜感激.

Any suggestions will be appreciated. J.

推荐答案

只需共享我的解决方案:

Just sharing my solution:

Dictionary<string, string> sdkConfig = new Dictionary<string, string>();
sdkConfig.Add("mode", "sandbox");

string clientid = "<your client id>";
string secretid = "<your secret id>";
string accessToken = new OAuthTokenCredential(clientid, secretid, sdkConfig).GetAccessToken();

我以前使用RestSharp遇到未经授权的响应,然后找到了.我正在使用nuget包中的paypal .net sdk. 参考.

I previously encountered unauthorized response using RestSharp, then found this. I'm using paypal .net sdk from nuget package. Reference.

这篇关于如何使用REST请求获取Paypal访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆