获取 paypal rest api grails 的访问令牌

Getting access token for paypal rest api grails

如何在 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 中获取身份验证令牌。它在终端中工作没有任何问题。

我正在尝试使用 HttpClient 来实现它,但如何传递用户凭据。在上面的例子中指定了 -u uname:pass in HttpClient.?

您似乎正在使用 Basic Auth 进行身份验证(参见 how to set basic auth in curl and 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 标志中拥有的值)或来自一对用户名和密码。