如何在 grails 中使用 paypal rest api
curl -v 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"
文档:https://developer.paypal.com/webapps/developer/docs/integration/mobile/verify-mobile-payment/
我需要在 grails 中获取身份验证 token 。它在终端中正常工作。
我正在尝试使用 HttpClient
来实现它,但如何传递用户凭据。在上面的例子中用 -u uname:pass
在 HttpClient
.?
最佳答案
您似乎正在使用 Basic Auth 进行身份验证(请参阅 how to set basic auth in curl 和 the client's section for Basic Auth in the Wikipedia)。
Apache 的 Http Client 为您提供身份验证处理,但在这种情况下,手动创建 header 似乎更容易:
Request addBasicAuthHeader(String username, String password) {
return addBasicAuthHeader(createBasicAuth(username, password))
}
Request addBasicAuthHeader(String headerValue) {
request.addHeader("Authorization", "Basic ${headerValue}")
return request
}
private String createBasicAuthHeader(String username, password) {
"$username:$password".toString().bytes.encodeBase64()
}
您可以调用任何 addBasicAuthHeader
方法(可能使它们适应您正在使用的 HttpClient
的风格),允许您从原始的添加基本身份验证 header 值(您在 curl
的 -u
标志中拥有的值)或来自一对用户名和密码。
关于grails - 获取 paypal rest api grails 的访问 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31786032/