Lisa Anne
Lisa Anne

Reputation: 4595

Please how do I port this Curl request to Android?

I am trying to get, form my Android Application, the PayPal Oauth refresh_token.

Like in this link https://github.com/paypal/PayPal-Android-SDK/blob/master/docs/future_payments_server.md#request

The request to achieve that is, in Curl, the following:

curl 'https://api.paypal.com/v1/oauth2/token' \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Authorization: Basic QWZV...==" \
-d 'grant_type=authorization_code&response_type=token&redirect_uri=urn:ietf:wg:oauth:2.0:oob&code=EBYhRW3ncivudQn8UopLp4A28...'

Please how do i do it form my Android App?

I have tried this way:

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("https://api.paypal.com/v1/oauth2/token");

    try {
        httppost.addHeader("Content-Type", "x-www-form-urlencoded");
        httppost.addHeader("Authorization", "Basic QWZV...==");
        StringEntity se=new StringEntity("grant_type=authorization_code&response_type=token&redirect_uri=urn:ietf:wg:oauth:2.0:oob&code="+authorization.getAuthorizationCode());
        httppost.setEntity(se);

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }

But I get a 400 BAD REQUEST response.

Upvotes: 0

Views: 553

Answers (1)

Sabuj Hassan
Sabuj Hassan

Reputation: 39365

The Content-Type header you are trying is completely wrong. It is not posting any data -d to the server. So you get bad request error.

Replace your one:

httppost.addHeader("Content-Type", "x-www-form-urlencoded");

With below one(also notice the difference between the two):

httppost.addHeader("content-type", "application/x-www-form-urlencoded");

Upvotes: 1

Related Questions