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アプリケーションでは、正しいユーザー名/パスワードを送信しなかったというエラーメッセージが表示されますが、テストアプリケーションの同じデータでアクセストークンが返されます。おそらくここで間違っている可能性がありますか?
私は数時間以上何とかこの作品を作ってみましたがちょうどこのような何かを書くために方法を見つけることができませんでした - あなたは一例を投稿してくださいできますか? –
@ M.Reiherスニペットで更新しました。 –