我正在使用Google API Client Library for Java来获取有关在我的Android应用程序中购买的用户订阅的信息.这就是我现在正在做的事情:
HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
JsonFactory JSON_FACTORY = new JacksonFactory();
GoogleCredential credential = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(GOOGLE_CLIENT_MAIL)
.setServiceAccountScopes("https://www.googleapis.com/auth/androidpublisher")
.setServiceAccountPrivateKeyFromP12File(new File(GOOGLE_KEY_FILE_PATH))
.build();
Androidpublisher publisher = new Androidpublisher.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).
setApplicationName(GOOGLE_PRODUCT_NAME).
build();
Androidpublisher.Purchases purchases = publisher.purchases();
Get get = purchases.get("XXXXX", subscriptionId, token);
SubscriptionPurchase subscripcion = get.execute(); //Exception returned here
Run Code Online (Sandbox Code Playgroud)
GOOGLE_CLIENT_MAIL是来自Google控制台的API Access的电子邮件地址.
GOOGLE_KEY_FILE_PATH是从API Access下载的p12文件.
GOOGLE_PRODUCT_NAME是品牌信息的产品名称.
在Google APIS控制台中,启用了"Google Play Android Developer API"服务.
我得到的是:
{
"code" : 401,
"errors" : [ {
"domain" : "androidpublisher",
"message" …Run Code Online (Sandbox Code Playgroud) 截至昨天,我的应用程序能够通过oAuth 2.0与google(youtube)进行身份验证,第一次没问题,但第二次(重新身份验证,相同的应用程序+同一用户),当我为accessToken交换requestToken时,我得到了一个错误:
error : invalid_grant
Run Code Online (Sandbox Code Playgroud)
我正在使用:
grant_type = authorization_code
Run Code Online (Sandbox Code Playgroud)
他们建议的.过去两天前发生的事情是重新验证,网页会说"您之前已使用此应用程序进行过身份验证,是否要再次授予其访问权限?".改变了什么或者我做错了什么?
最后,我可以解决我最近处理用于访问google API的OAuth2身份验证的问题.
但现在我在查询android开发人员API以检索有关订阅的状态信息时遇到困难.我正在使用以下REST API调用:
https://www.googleapis.com/androidpublisher/v1/applications/ APP_PACKAGE/subscriptions/SUBSCRIPTION_ID/purchases/PURCHASE_TOKEN?access_token = AUTH_TOKEN
我检索此请求的身份验证令牌如下:
// Build the HTTP parameter
Map<String,String> params = [:]
params.put("grant_type", "refresh_token")
params.put("refresh_token", googleRefreshToken.encodeAsURL())
params.put("client_id", googleClientId.encodeAsURL())
params.put("client_secret", googleClientSecret.encodeAsURL())
// Send the POST request
// This action might throw an exception in case any parameter were wrong, invalid or not specified.
String result = HttpRequestHandler.sendRequest("https://accounts.google.com/o/oauth2/token", params);
JSONElement jsonResult = JSON.parse(result)
Run Code Online (Sandbox Code Playgroud)
这工作得很好,但上面提到的REST调用返回500错误 - …