Question

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"}
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top