2

Hi and Thank you for your help.

From my Android App I try to get PayPal Oauth refresh_token.

The Curl code is:

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...'

I do like this.

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

try {
String text=CONFIG_CLIENT_ID+":"+SECRET;
        byte[] data = text.getBytes("UTF-8");
        String base64 = Base64.encodeToString(data, Base64.DEFAULT);

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

        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 the following response:

Invalid URL

The requested URL "/v1/oauth2/token", is invalid.


EDIT EDIT EDIT EDIT EDIT EDIT EDIT

If I use

HttpPost httppost = new HttpPost("https://api.sandbox.paypal.com")

I get:

Invalid URL

The requested URL "/", is invalid.

Reference #9.8c5e6cc1.1395837240.16f01684


EDIT EDIT EDIT EDIT EDIT EDIT EDIT

The following code seems to be working,

thanks to Sabuj Hassan answer.

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

try {
String text=CONFIG_CLIENT_ID+":"+SECRET;
        byte[] data = text.getBytes("UTF-8");
        String base64 = Base64.encodeToString(data, Base64.NO_WRAP);

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

        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 now I get the following response from the PayPal sandbox server:

{"error":"invalid_request","error_description":"Invalid auth code"}
Lisa Anne
  • 4,482
  • 17
  • 83
  • 157
  • `https://api.sandbox.paypal.com/v1/oauth2/token` is correct, if you leave out `v1/oauth2/token` then go to the website using your browser, it will give 404 error. But the reason this ain't working is probably because you are trying to post data to an `https` url, instead of a normal `http`. It is a bit more complicated and you could ignore the SSL certificate, but this is not recommendable with respect to security. See: http://stackoverflow.com/questions/5206010/using-apache-httpclient-for-https – Yeti Mar 26 '14 at 22:53
  • hello can you tell what is authorization.getAuthorizationCode() – Aditya Vyas-Lakhan Aug 12 '16 at 03:51

1 Answers1

2

Your code seems ok with me. It can send data to my hosted server nicely. So I suspect the problem is with your following line:

String base64 = Base64.encodeToString(data, Base64.DEFAULT);

Print the base64 and see whether its the exact match of the header from your curl's command. I want to believe that its different. Possibly its added with newline character(in base64).

So use this one:

String base64 = Base64.encodeToString(data, Base64.NO_WRAP);

See the documentation for other encoding flags.

Sabuj Hassan
  • 38,281
  • 14
  • 75
  • 85
  • thanks Sabuj, that seems to be working, but now I get the following response `{"error":"invalid_request","error_description":"Invalid auth code"}` Please any idea??? – Lisa Anne Mar 27 '14 at 10:33
  • I'll check it later. By this time please update your code with current one. Also, by any chance `authorization.getAuthorizationCode()` needs to be urlencoded?? Does it have any character like `&` `=` `?` etc? – Sabuj Hassan Mar 27 '14 at 10:38
  • thanks Sabuj, I have tried to urlencode, but same result :-((( ... thanks a lot for your help! – Lisa Anne Mar 27 '14 at 10:53
  • 1
    SUCCESS!!!, I switched the PayPal configuration to ENVIRONMENT_SANDBOX and logged in with the sandbox account and I got the refresh and access tokens! THANKS Sabuj!!!! – Lisa Anne Mar 27 '14 at 11:11
  • @SabujHassan can you help me with paypal – Aditya Vyas-Lakhan Aug 12 '16 at 03:43