【问题标题】:Google androidpublish api access_token谷歌 androidpublish api access_token
【发布时间】:2023-03-26 13:29:01
【问题描述】:

我正在开发一个带有 Google Play 应用内购买的 Android 应用程序。我还有一个在后台工作的服务器应用程序(JAVA)。每当用户在应用程序中成功购买(购买正在工作)时,服务器应用程序应向 Google Play 发送请求以验证购买。

Google Play 为此提供了 androidpublisher api: developer.google.com/android-publisher/getting_started

问题在于 access_token。每当我获得一个新的 access_token(使用刷新或新令牌请求方法)时,如果我尝试使用 GET 方法从 Google Play 商店的购买 inapp 项目中询问信息,它就不起作用(https://developers.google.com/android-publisher/v1/purchases/get) -> 它总是导致错误 401(无效凭据)或 403(未配置访问)。

经过无休止的尝试,我发现我只能使用该链接:https://developers.google.com/apis-explorer/#p/androidpublisher/v1.1/androidpublisher.inapppurchases.get,并且我只能从那里读取 valid access_token,并且只有使用该令牌我才能让我的服务器应用程序工作但只工作一小时,一小时后我必须再次执行相同的步骤...

现在我真的需要一种正确的方法来通过我的应用程序获取有效的 access_token

我在互联网上进行了很多研究,但找不到有用的东西。 我知道我不是唯一一个遇到这个问题的人,也许你们中的一些人知道该怎么做或者已经有经验

【问题讨论】:

    标签: java android in-app-purchase


    【解决方案1】:

    您可以使用com.google.api-clientgoogle-api-services-androidpublisher 库。

    首先在谷歌开发者控制台(https://console.developers.google.com)上进入项目

    • API 和身份验证 -> API
    • 启用“Google Play Android 开发者 API”
    • 转到凭据 -> 创建新的客户端 ID
    • 选择服务帐号
    • 创建客户端 ID
    • 将 p12 文件保存在安全的地方

    然后将刚刚生成的服务帐户电子邮件地址添加到您的 google play 开发者控制台 (https://play.google.com/apps/publish/)

    • 设置 -> 用户帐户和权限 -> 邀请新用户
    • 粘贴@developer.gserviceaccount.com邮箱账号
    • 选择“查看财务报告”
    • 发送邀请

    现在看代码。将以下依赖项添加到您的 pom.xml 文件中:

    <dependency>
        <groupId>com.google.api-client</groupId>
        <artifactId>google-api-client</artifactId>
        <version>1.18.0-rc</version>
    </dependency>
    <dependency>
        <groupId>com.google.http-client</groupId>
        <artifactId>google-http-client-jackson2</artifactId>
        <version>1.18.0-rc</version>
    </dependency>
    <dependency>
        <groupId>com.google.apis</groupId>
        <artifactId>google-api-services-androidpublisher</artifactId>
        <version>v1.1-rev25-1.18.0-rc</version>
    </dependency>
    

    然后先验证签名:

    byte[] decoded = BASE64DecoderStream.decode(KEY.getBytes());
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    PublicKey publicKey = keyFactory.generatePublic(new X509EncodedKeySpec(decoded));
    Signature sig = Signature.getInstance("SHA1withRSA");
    sig.initVerify(publicKey);
    sig.update(signedData.getBytes());
    if (sig.verify(BASE64DecoderStream.decode(signature.getBytes())))
    {
        // Valid
    }
    

    如果签名验证获取订阅详细信息:

    // fetch signature details from google
    HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    GoogleCredential credential = new GoogleCredential.Builder()
        .setTransport(httpTransport)
        .setJsonFactory(jsonFactory)
        .setServiceAccountId(ACCOUNT_ID)
        .setServiceAccountScopes(Collections.singleton("https://www.googleapis.com/auth/androidpublisher"))
        .setServiceAccountPrivateKeyFromP12File(new File("key.p12"))
        .build();
    
    AndroidPublisher pub = new AndroidPublisher.Builder(httpTransport, jsonFactory, credential)
        .setApplicationName(APPLICATION_NAME)
        .build();
    AndroidPublisher.Purchases.Get get = pub.purchases().get(
        APPLICATION_NAME,
        PRODUCT_ID,
        token);
    SubscriptionPurchase subscription = get.execute();
    System.out.println(subscription.toPrettyString());
    

    这将通过生成 JWT 令牌来处理所有令牌问题,因此您不必自己处理。

    【讨论】:

    • 非常感谢你,我得到了它与你的代码一起工作:) 我不明白的是上面的代码你为什么要实施验证,你的意思是哪个“签名”以及什么是变量“KEY”应该是?
    • 我收到"The current user has insufficient permissions to perform the requested operation.。有没有可能的解决方案。我检查了用户是否具有财务访问权限(也通过授予管理员访问权限进行了尝试)。此外,我正在使用 .p12 键,如所述。 Json 是否有解决方案(根据指南推荐)?我们是否必须指定 .p12 密钥密码?
    • @SukhmeetSingh 你可以像这样使用json文件:GoogleCredential.fromStream(new FileInputStream("xxx.json"), transport, jsonFactory).createScoped(scope);
    猜你喜欢
    • 1970-01-01
    • 2019-10-28
    • 2021-07-28
    • 2018-09-16
    • 1970-01-01
    • 1970-01-01
    • 2018-11-08
    • 2012-05-13
    • 2019-03-20
    相关资源
    最近更新 更多