请问如何将此Curl请求移植到Android?

时间:2014-03-22 16:51:53

标签: java android curl

我想从我的Android应用程序中获取PayPal Oauth refresh_token。

与此链接一样https://github.com/paypal/PayPal-Android-SDK/blob/master/docs/future_payments_server.md#request

实现这一目标的要求是,在Curl中,以下内容:

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

请问如何从我的Android应用程序中进行操作?

我试过这种方式:

    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
    }

但我收到400 BAD REQUEST回复。

1 个答案:

答案 0 :(得分:1)

您正在尝试的Content-Type标头是完全错误的。它不会向服务器发布任何数据-d。所以你得到了错误的请求错误。

替换你的:

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

低于一(也注意两者之间的差异):

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