2017-11-30 14 views
0

Android 애플리케이션에서 Salesforce를 사용할 수없는 이유를 찾는 데 문제가 있습니다. 함수는 다음과 같습니다이 어떻게 든 작동AsyncHttpClient가 HttpClient에서 Salesforce에 로그인 할 때와 동일한 데이터로 작동하지 않습니다.

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(); 
     } 
    } 

을 :

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()); 
     } 
    }); 

나는이처럼 보이는, 동일한 데이터를 취하는 간단한 자바 프로그램에서 동일한 논리를 구축했습니다. 안드로이드 응용 프로그램에서 올바른 사용자 이름/암호를 전송하지 못했다는 오류 메시지가 표시되지만 테스트 응용 프로그램의 동일한 데이터를 사용하여 액세스 토큰을 다시 가져옵니다. 여기에 무엇이 잘못 될 수 있습니까?

답변

0

Java HttpPost와 Android AsyncHttpClient 라이브러리 간에는 큰 차이가 있습니다.

콘텐츠 유형 헤더를 추가해 보셨습니까? 아니면 적어도 헤더 디버깅?

나는 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 스 니펫으로 업데이트했습니다. –