2017-11-30 3 views
0

AndroidアプリケーションでSalesforceにアクセスできない理由を見つけようとしています。次のように関数は次のとおりです。AsyncHttpClientがHttpClientと同じデータで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アプリケーションでは、正しいユーザー名/パスワードを送信しなかったというエラーメッセージが表示されますが、テストアプリケーションの同じデータでアクセストークンが返されます。おそらくここで間違っている可能性がありますか?

答えて

0

Java HttpPostとAndroid AsyncHttpClientライブラリには大きな違いがあります。

Content Typeヘッダーを追加しようとしましたか?または少なくともヘッダーをデバッグしますか?

OAuthリクエストでは、私は通常{ Content-Type : "application/x-www-form-urlencoded" }を使用して、POST本体の内部にデータを渡します。このような

何か:

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)); 
+0

私は数時間以上何とかこの作品を作ってみましたがちょうどこのような何かを書くために方法を見つけることができませんでした - あなたは一例を投稿してくださいできますか? –

+0

@ M.Reiherスニペットで更新しました。 –

関連する問題