我正在尝试使用Scala中的Playframework进行WS GET调用,以调用Paypal REST JSON API。我更具体地尝试get the initial Paypal access token:
http://localhost:3000
我这样做是在Scala中构建的:
curl -v https://api.sandbox.paypal.com/v1/oauth2/token \
-H "Accept: application/json" \
-H "Accept-Language: en_US" \
-u "client_id:secret" \
-d "grant_type=client_credentials"
我在这里假设原始@Inject(ws: WSClient)
val url = config.get[String]("paypal.url.token") // https://api.sandbox.paypal.com/v1/oauth2/token
val httpHeaders = Array(
"Accept" -> "application/json",
"Accept-Language" -> "en_US"
)
val username = config.get[String]("paypal.client_id")
val password = config.get[String]("paypal.secret")
val request: WSRequest = ws.url(url).withHttpHeaders(httpHeaders: _*).
withRequestTimeout(timeout).
withAuth(username, password, WSAuthScheme.BASIC)
val futureResponse = request.get()
futureResponse.map { response =>
println(response.json)
}
中的-u
意味着并对应于curl
,但是我不知道withAuth
的{{1}}对应什么来吗?
当我像现在这样运行时,出现以下错误:
-d
和一些在日志之前的时间:
grant_type=client_credentials
答案 0 :(得分:2)
您可以使用curl
阅读man curl
手册。
所以,
1)-u
代表--user <username:passsword>
-u, --user <user:password> Specify the user name and password to use for server authentication. Overrides -n, --netrc and --netrc-optional. If you simply specify the user name, curl will prompt for a password.
这也可以翻译为-H "Authorization: <Basic|Bearer> base64_for_username:password"
2)-d
代表POST请求中的--data
或有效负载。
-d, --data <data> (HTTP) Sends the specified data in a POST request to the HTTP server, in the same way that a browser does when a user has filled in an HTML form and presses the submit button. This will cause curl to pass the data to the server using the con- tent-type application/x-www-form-urlencoded. Compare to -F, --form.
withAuth(username, password, WSAuthScheme.BASIC)
是否使用了正确的用户名和密码答案 1 :(得分:1)
尝试使用post
而不是像这样的get
ws
.url(url)
.withAuth(username, password, WSAuthScheme.BASIC)
.post(Map("grant_type" -> Seq("client_credentials")))
.map(println)