Chr*_*ill 5 api android subscription in-app-billing
简而言之:我可以在服务器端使用Google Play Android Developer API,而无需在Play商店中提供任何应用程序吗?
背景:我正在开发一个项目,为每月订阅提供应用程序.每个订阅的相应数据(购买令牌,日期等)存储在后端数据库中.现在我想创建一个循环遍历每个数据集的cronjob.对于每个订阅,如果订阅仍然有效,我想联系Google API以检索信息,并更新与响应状态相对应的数据库.
对于后端逻辑,我使用google-api-java-client库.
要取消或验证订阅,我需要先使用OAuth2对自己进行身份验证.去过也做过.
new GoogleCredential.Builder()
.setTransport(HTTP_TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setServiceAccountScopes("https://www.googleapis.com/auth/androidpublisher") // $1
.setServiceAccountPrivateKeyFromP12File(new File(filePath))
.setClientSecrets(CLIENT_ID, CLIENT_SECRET) // $2
.build();
Run Code Online (Sandbox Code Playgroud)
1美元:我不知道给定的帐户范围是否有效.因为我只是在极少数示例中找到了这个值,但在本概述和谷歌操场中都没有
$ 2我想这是必要的,尽管我发现很多例子没有提供这些信息.
但是,遗憾的是,当我提供无效数据(如错误的电子邮件或私钥)时,我看不出任何差异.
问题
在下一步中,我尝试获取订阅的购买状态:
Androidpublisher publisher = new Androidpublisher.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(GOOGLE_PRODUCT_NAME) // $1
.build();
Androidpublisher.Purchases purchases = publisher.purchases();
Androidpublisher.Purchases.Get get = purchases.get("android.test.purchased", "monthly001", "mytoken"); // $2
SubscriptionPurchase subscripcion = get.execute();
Run Code Online (Sandbox Code Playgroud)
$ 1:来自API控制台的虚拟产品名称- > API Access
$ 2:除了事实之外,androidpush API 不允许通过服务帐户联系它,但只能通过web服务器应用程序auth flow,我没有任何线索在get-方法的参数中插入什么.
以下是API:https: //developers.google.com/android-publisher/v1/purchases/get
问题
阅读本文后,我知道有办法处理假/静态响应.但是,如果这也适用于订阅,或者只是仅针对移动设备上的应用内帐单,我无法阅读.
我想知道为什么/如果有任何简单的方法用沙盒或s.th.开发.simliar.
我仍然觉得我只是缺少很大的一部分来理解事情应该如何运作.也许你们中的某个人可以给我一个如何在这个地方继续前进的提示,或者可以说我在哪里错了.
亲切的问候,
克里斯托弗
我现在可以解决我以前的大部分理解问题了。
=1= 生成授权 URL
String authorizeUrl = new GoogleAuthorizationCodeRequestUrl(googleClientId,callbackUrl,"https://www.googleapis.com/auth/androidpublisher").build()
// See why: http://stackoverflow.com/questions/8433990/when-authenticating-with-oauth-and-youtube-always-get-error-invalid-grant-on
authorizeUrl += "&approval_prompt=force&access_type=offline"
Run Code Online (Sandbox Code Playgroud)
=2= 验证
由于 server-webflow 不适用于 androidpublisher API,因此客户现在必须手动调用 (1) 中生成的 URL。
=3= 回调
谷歌回调应该处理接下来的步骤。回调包含我们必须使用的参数“code”。
=4= 请求身份验证令牌
// Build the HTTP parameter
Map<String,String> params = [:]
params.put("grant_type", "authorization_code")
params.put("code", code.encodeAsURL())
params.put("client_id", customer.googleClientId.encodeAsURL())
params.put("client_secret", customer.googleClientSecret.encodeAsURL())
params.put("redirect_uri", getCallbackUrl().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)
// Map result
OAuth2Result oAuth2Result = new OAuth2Result()
oAuth2Result.accessToken = jsonResult.getAt("access_token")
oAuth2Result.refreshToken = jsonResult.getAt("refresh_token")
oAuth2Result.ttlSeconds = Integer.parseInt(jsonResult.getAt("expires_in").toString())
oAuth2Result.tokenType = jsonResult.getAt("token_type")
Run Code Online (Sandbox Code Playgroud)
=5= 请求刷新令牌
// Build the HTTP parameter
Map<String,String> params = [:]
params.put("grant_type", "refresh_token")
params.put("refresh_token", this.customer.googleRefreshToken.encodeAsURL())
params.put("client_id", customer.googleClientId.encodeAsURL())
params.put("client_secret", customer.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)
// Map result
OAuth2Result oAuth2Result = new OAuth2Result()
oAuth2Result.accessToken = jsonResult.getAt("access_token")
oAuth2Result.refreshToken = jsonResult.getAt("refresh_token")
oAuth2Result.ttlSeconds = Integer.parseInt(jsonResult.getAt("expires_in").toString())
oAuth2Result.tokenType = jsonResult.getAt("token_type")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5061 次 |
| 最近记录: |