skip to Main Content

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


  1. You can read curl manual with man curl.

    So,
    1) -u represents --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.
    

    This also translates to -H "Authorization: <Basic|Bearer> base64_for_username:password"

    2) -d represents the --data or payload on POST request.

       -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.
    
    Login or Signup to reply.
  2. Try post instead of a get like so

    ws
      .url(url)
      .withAuth(username, password, WSAuthScheme.BASIC)
      .post(Map("grant_type" -> Seq("client_credentials")))
      .map(println)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search