向Google Play Android开发者API发出HTTP发布请求

向Google Play Android开发者API发出HTTP发布请求,第1张

概述我正在尝试授权GooglePlayAndroid开发人员API.我现在需要发出一个HTTP发布请求,以将访问令牌和刷新令牌的授权代码交换.Google给出以下示例请求:POST/o/oauth2okenHTTP/1.1Host:accounts.google.comContent-Type:application/x-www-form-urlencodedcode=4/v6xr77ewYq

我正在尝试授权Google Play Android开发人员API.我现在需要发出一个http发布请求,以将访问令牌和刷新令牌的授权代码交换. Google给出以下示例请求:

POST /o/oauth2/token http/1.1Host: accounts.Google.comContent-Type: application/x-www-form-urlencodedcode=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”错误.

解决方法:

这就是我解决的方法.我最终使用了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);
总结

以上是内存溢出为你收集整理的向Google Play Android开发者API发出HTTP发布请求全部内容,希望文章能够帮你解决向Google Play Android开发者API发出HTTP发布请求所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1085622.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存