【发布时间】:2012-08-31 12:01:16
【问题描述】:
我正在尝试授权 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”错误。
【问题讨论】:
-
您已经在问题中回答了您的问题 :-) stackoverflow.com/questions/4543894/… 找到相似之处 :-) ...但我认为您不需要 redirect_uri 参数
-
你能至少告诉我我给出的两个选项中的哪一个吗?已经三天了...拜托...
-
您看到stackoverflow.com/questions/4543894/… ... 评分最高的答案了吗?
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(x); nameValuePairs.add(new BasicNameValuePair("code", "4/v6xr77ewYqhvHSyW6UJ1w7jKwAzu")); nameValuePairs.add(new BasicNameValuePair("client_id", "8819981768.apps.googleusercontent.com"));/*...rest of the params...*/ httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));HttpResponse response = httpclient.execute(httppost);那么你应该使用 HttpEntity 将响应转换为字符串,然后解析 json -
我合作过吗? ... to wszystko było w Twoim pytaniu :)
-
Dalej nie... caly czas dostaje "error: invalid_request" albo "error: invalid_grant" albo "error: invalid_client"...
标签: android api http-post httprequest google-play