服务帐户和Google Play开发人员API

时间:2018-10-10 07:01:21

标签: google-api-java-client

https://developer.android.com/google/play/billing/billing_best_practices

所述,我尝试验证服务器上的移动用户购买情况

我遇到了一个臭名昭著的错误:“用于调用Google Play开发者API的项目ID尚未在Google Play开发者控制台中链接。”

在大量研究SO,Google等之后,...仍然有问题,我寻求您的帮助。

这是我的设置:

  1. 在我的G play控制台中配置了服务帐户: G play console

  2. 服务帐户具有权限: enter image description here

  3. 我已经从Google Developers Console下载了一个JSON密钥

  4. 我正在运行以下Kotlin代码

代码:

compile 'com.google.http-client:google-http-client-gson:1.25.0'
compile 'com.google.apis:google-api-services-androidpublisher:v3-rev30-1.25.0'


val httpTransport = GoogleNetHttpTransport.newTrustedTransport()
val jsonFactory = GsonFactory.getDefaultInstance()
val applicationName = "MYAPP"
val packageName = "my.package"
val json = "copy/paste json key here"
val credential = GoogleCredential.fromStream(StringInputStream(json), httpTransport, jsonFactory)
      .createScoped(listOf((AndroidPublisherScopes.ANDROIDPUBLISHER)))
val publisher = AndroidPublisher.Builder(httpTransport, jsonFactory, credential)
      .setApplicationName(applicationName)
      .build()

val productId = "product_id"
val userPurchaseToken = "a_token"
val get = publisher.purchases()
        .products()
        .get(packageName, productId, userPurchaseToken)
val purchase = get.execute()
Logger.getLogger("Payment user").info("Found google purchase item ${purchase.toPrettyString()}")

然后我得到了异常

com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
  "code": 403,
  "errors": [
    {
      "domain": "androidpublisher",
      "message": "The project id used to call the Google Play Developer API has not been linked in the Google Play Developer Console.",
      "reason": "projectNotLinked"
    }
  ],
  "message": "The project id used to call the Google Play Developer API has not been linked in the Google Play Developer Console."
}

at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:150)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1067)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)

请注意,我已经等待了24小时,但仍然遇到相同的错误。

  1. 非常奇怪的是,我设法在https://github.com/googlesamples/android-play-publisher-api/blob/master/v3/java/src/com/google/play/developerapi/samples/ListApks.java的代码示例中列出了APK。

我在这里完全迷路了...

为什么我的密钥可用于列出APK,但无法验证购买?

编辑:添加了第5步

1 个答案:

答案 0 :(得分:0)

对我来说,这部分有所帮助。

If you get the same error again, maybe is because you have configured and 
purchased products in your console before linking the project.

To solve it, simply add new product in your app.

从此答案 https://stackoverflow.com/a/25655897/5169529