我正在尝试授权 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”错误。