3

Google Play Android Developer API を承認しようとしています。アクセス トークンとリフレッシュ トークンの認証コードを交換するために、HTTP ポスト リクエストを作成する必要がある段階にいます。Googleは次の例のリクエストを提供します。

POST /o/oauth2/token HTTP/1.1
Host: accounts.google.com
Content-Type: application/x-www-form-urlencoded

code=4/v6xr77ewYqhvHSyW6UJ1w7jKwAzu&
client_id=8819981768.apps.googleusercontent.com&
client_secret={client_secret}&
redirect_uri=https://oauth2-login-demo.appspot.com/code&
grant_type=authorization_code

私は混乱しています...まず、インストールされたアプリケーション(Android)の場合、 client_secret は指定されていません。Google API コンソールで同じプロジェクトの Web アプリケーションを作成したところ、client_secret が得られたので、Web アプリケーションがなくてもそれを使用しました。次のコードでは、「invalid_grant」エラーが発生します。

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https://accounts.google.com/o/oauth2/token");

try {
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
    nameValuePairs.add(new BasicNameValuePair("code", "CODE"));
    nameValuePairs.add(new BasicNameValuePair("client_id", "CLIENT_ID"));
    nameValuePairs.add(new BasicNameValuePair("client_secret", "CLIENT_SECRET"));
    nameValuePairs.add(new BasicNameValuePair("redirect_uri", "urn:ietf:wg:oauth:2.0:oob"));
    nameValuePairs.add(new BasicNameValuePair("grant_type", "authorization_code"));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    HttpResponse response = httpclient.execute(httppost);
    ....

client_secret を完全に取り出すと、「invalid_request」エラーが発生しました。

4

2 に答える 2

3

これが私がそれを解決した方法です。最終的にWebアプリケーションを使用しました。詳細については、こちらの回答をご覧ください。

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("https://accounts.google.com/o/oauth2/token");

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("grant_type",    "refresh_token"));
nameValuePairs.add(new BasicNameValuePair("client_id",      CLIENT_ID));
nameValuePairs.add(new BasicNameValuePair("client_secret",  CLIENT_SECRET));
nameValuePairs.add(new BasicNameValuePair("refresh_token",  REFRESH_TOKEN));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

HttpResponse response = client.execute(post);
于 2012-10-25T17:12:12.263 に答える
1

インストールされたアプリケーションには適用されないため、 client_secret キーを削除するだけで、Web アプリケーションの助けを借りずに Android アプリからアクセス トークンのアクセス コードを引き換えることができました。

HttpPost httppost = new HttpPost("https://accounts.google.com/o/oauth2/token");
httppost.setHeader("Content-type", "application/x-www-form-urlencoded");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("grant_type",    "authorization_code"));
nameValuePairs.add(new BasicNameValuePair("client_id",      BLOGGER_CLIENT_ID));
nameValuePairs.add(new BasicNameValuePair("redirect_uri",  "http://localhost"));
nameValuePairs.add(new BasicNameValuePair("code", BLOGGER_ACCESS_CODE));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpClient httpClient = new DefaultHttpClient(myParams);
response = httpClient.execute(httppost);
String returnedJsonStr = EntityUtils.toString(response.getEntity());
JSONObject jsonObject = new JSONObject(returnedJsonStr);
String receivedToken = jsonObject.getString("access_token");

このコメントを投稿する理由は、あなたのソリューションが、モバイル アプリでアクセス トークンを取得する唯一の方法は Web アプリケーションを介することだと考えている人に誤解を与える可能性があるためです。

invalid_grant エラーを回避するには、次のコードに従ってください: https://stackoverflow.com/a/14141020/989418

于 2013-01-03T10:39:17.400 に答える