ASYNCHTTPCLEINT与HTTPCLIENT中的数据不使用以登录Salesforce



我很难找出为什么我无法在Android应用程序中获得Salesforce。该功能如下:

RequestParams params = new RequestParams();
    params.put("grant_type", "password");
    params.put("client_id", API_KEY);
    params.put("client_secret", SECRET_KEY);
    params.put("username", USERNAME);
    params.put("password", String.format("%s%s", PASSWORD, SECURITY_CODE));
    //params.put("username", login);
    //params.put("password", password);
    AsyncHttpClient client = new AsyncHttpClient();
    client.post("https://login.salesforce.com/services/oauth2/token", params, new JsonHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
            String accessToken;
            String instanceUrl;
            String tokenType;
            String signature;
            try {
                accessToken = response.getString("access_token");
                instanceUrl = response.getString("instance_url");
                tokenType = response.getString("token_type");
                signature = response.getString("signature");
            } catch (JSONException e) {
                callback.onFailure("Error by parsing...");
                return;
            }
            /**
             * After getting the main credential data, we save them in SFSession.
             */
            SFSession.setAccessToken(context, accessToken);
            SFSession.setInstanceUrl(context, instanceUrl);
            SFSession.setTokenType(context, tokenType);
            SFSession.setSignature(context, signature);
            callback.onSuccess();
        }
        @Override
        public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject response) {
            //If the app crashes right here it might just be that you forgot to turn on WiFi.
            callback.onFailure(response.toString());
            Log.e("ERROR", response.toString());
        }
    });

我尝试在使用相同数据的简单Java应用程序中构建相同的逻辑,看起来像这样:

HttpClient httpclient = HttpClients.createDefault();
    HttpPost httppost = new HttpPost("https://login.salesforce.com/services/oauth2/token");
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("grant_type", "password"));
    params.add(new BasicNameValuePair("client_id", API_KEY));
    params.add(new BasicNameValuePair("client_secret", SECRET_KEY));
    params.add(new BasicNameValuePair("username", USERNAME));
    params.add(new BasicNameValuePair("password", String.format("%s%s", PASSWORD, SECURITY_CODE)));
    httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
    HttpResponse response = httpclient.execute(httppost);
    System.out.println(response.getProtocolVersion());
    System.out.println(response.getStatusLine().getStatusCode());
    System.out.println(response.getStatusLine().getReasonPhrase());
    System.out.println(response.getStatusLine().toString());
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        InputStream instream = entity.getContent();
        try {
            java.util.Scanner s = new java.util.Scanner(instream).useDelimiter("\A");
            System.out.print(s.next());
        } finally {
            instream.close();
        }
    }

这个有效。在Android应用程序中,我收到了一条错误消息,因为没有发送正确的用户名/密码,但是在测试应用程序中使用相同的数据,我会返回访问令牌。这里可能出问题了?

Java Httppost和Android asynchttpclient库之间有很大的区别。

您是否尝试过添加内容类型标头?或至少调试标题?

使用OAuth请求,我通常使用{ Content-Type : "application/x-www-form-urlencoded" },然后将数据传递到帖子主体内。

类似的东西:

HttpPost httppost = new HttpPost("https://test.salesforce.com/services/oauth2/token");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
nameValuePairs.add(new BasicNameValuePair("grant_type", "grant_type"));
nameValuePairs.add(new BasicNameValuePair("client_id", "client_id"));
nameValuePairs.add(new BasicNameValuePair("client_secret", "client_secret")); 
nameValuePairs.add(new BasicNameValuePair("username", "username"));
nameValuePairs.add(new BasicNameValuePair("password", "password"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

最新更新