I’m trying to make a WS GET call using the Playframework in Scala to invoke the Paypal REST JSON API. I’m more specifically trying to get the initial Paypal access token:
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"
I build this in Scala by doing:
@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)
}
I here assumed that -u
in the original curl
sample meant and corresponded to withAuth
but I don’t know what the -d
for grant_type=client_credentials
corresponds to?
When I run it like it is now I get the following error:
{"error":"invalid_token","error_description":"Authorization header does not have valid access token"}
and some when before the log:
[info] p.s.a.o.a.n.h.i.Unauthorized401Interceptor - Can't handle 401 as auth was already performed
2
Answers
You can read
curl
manual withman curl
.So,
1)
-u
represents--user <username:passsword>
This also translates to
-H "Authorization: <Basic|Bearer> base64_for_username:password"
2)
-d
represents the--data
or payload on POST request.withAuth(username, password, WSAuthScheme.BASIC)
is taking right username, passwordTry
post
instead of aget
like so