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」エラーが発生しました。