gpt4 book ai didi

android - 向 Google Play Android Developer API 发出 HTTP post 请求

转载 作者:太空宇宙 更新时间:2023-11-03 13:31:40 31 4
gpt4 key购买 nike

我正在尝试授权 Google Play Android Developer API。我正处于需要发出 HTTP post 请求以将授权代码交换为访问 token 和刷新 token 的步骤。 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 控制台中为同一个项目创建了一个网络应用程序,这给了我一个 client_secret,所以我使用了它,即使没有网络应用程序。以下代码给我一个“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”错误。

最佳答案

我就是这样解决的。我最终使用了 Web 应用程序。在我的回复中查看更多详细信息 here .

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);

关于android - 向 Google Play Android Developer API 发出 HTTP post 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12303935/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com