2013-10-09 69 views
1

我試圖讓它工作https://developer.paypal.com/webapps/developer/docs/integration/direct/make-your-first-call/ 使用Java + Jersey應用程序。看起來我在POST params中錯過了一些東西。如何使用REST請求獲取貝寶訪問令牌

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未授權的響應狀態。

這curl命令行選項只是正常工作:

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" 

任何建議將不勝感激。 J.

回答

1

curl中的-u選項發送base64編碼的「username:password」字符串。我不認爲添加客戶端ID /祕密到queryParams地圖是一樣的(除非澤西對待'用戶名'鍵不同,我不認爲它)。

而應該嘗試

webResource.header( 「授權」, 「基本」 + Base64.encode(CLIENT_ID + 「:」 + SECRET.getBytes()))

1

只是分享我的解決方案:

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。 Reference.

+0

你能告訴如何進入android? –

+0

嘗試此[文章](https://techtrendzy.wordpress.com/2013/10/10/get-an-access-token-for-rest-api/)或[來自PayPal的示例](https:// devtools -paypal.com/guide/pay_creditcard/java?interactive=ON&env=sandbox)您將需要PayPal Java Rest SDK作爲第二個鏈接,不完全是Android示例,但它是用Java編寫的......如果你還沒有在Android上工作,教我Android也許我們可以更快地解決你的問題:)) – juniordeveloper

相關問題