【问题标题】:What is the best way to access Google Play Developers API with Node.js?使用 Node.js 访问 Google Play Developers API 的最佳方式是什么?
【发布时间】:2020-05-27 22:40:56
【问题描述】:

android-publisherAPI(用于验证 IAP)只有 Python 和 Java 客户端包装器。使用 Node.js 似乎唯一的方法是通过 http 但是,我想知道是否有一种方法可以使用 Google API Node.js 客户端生成 OAuth 服务到服务身份验证的授权令牌并将其传递给 http 调用.手动生成 JWT 令牌是一个艰巨而漫长的过程。有没有最佳实践/推荐的方法来做这个简单的方法?

【问题讨论】:

    标签: node.js google-api google-cloud-functions google-play


    【解决方案1】:

    Eldermao,只需使用 google.auth.GoogleAuth 获取 JWT 客户端以通过 Google API 进行身份验证。这是来自谷歌代码的示例:

    // Before running the sample:
     * // - Enable the API at:
     * //   https://console.developers.google.com/apis/api/androidpublisher.googleapis.com
     * // - Login into gcloud by running:
     * //   `$ gcloud auth application-default login`
     * // - Install the npm module by running:
     * //   `$ npm install googleapis`
     *
     * const {google} = require('googleapis');
     * const androidpublisher = google.androidpublisher('v3');
     *
     * async function main() {
     *   const auth = new google.auth.GoogleAuth({
     *     // Scopes can be specified either as an array or as a single, space-delimited string.
     *     scopes: ['https://www.googleapis.com/auth/androidpublisher'],
     *   });
     *
     *   // Acquire an auth client, and bind it to all future calls
     *   const authClient = await auth.getClient();
     *   google.options({auth: authClient});
     *
     *   // Do the magic
     *   const res = await androidpublisher.purchases.subscriptions.get({
     *     // The package name of the application for which this subscription was purchased (for example, 'com.some.thing').
     *     packageName: 'placeholder-value',
     *     // The purchased subscription ID (for example, 'monthly001').
     *     subscriptionId: 'placeholder-value',
     *     // The token provided to the user's device when the subscription was purchased.
     *     token: 'placeholder-value',
     *   });
     *   console.log(res.data);
     *
     *   // Example response
     *   // {
     *   //   "acknowledgementState": 0,
     *   //   "autoRenewing": false,
     *   //   "autoResumeTimeMillis": "my_autoResumeTimeMillis",
     *   //   "cancelReason": 0,
     *   //   "cancelSurveyResult": {},
     *   //   "countryCode": "my_countryCode",
     *   //   "developerPayload": "my_developerPayload",
     *   //   "emailAddress": "my_emailAddress",
     *   //   "expiryTimeMillis": "my_expiryTimeMillis",
     *   //   "externalAccountId": "my_externalAccountId",
     *   //   "familyName": "my_familyName",
     *   //   "givenName": "my_givenName",
     *   //   "introductoryPriceInfo": {},
     *   //   "kind": "my_kind",
     *   //   "linkedPurchaseToken": "my_linkedPurchaseToken",
     *   //   "obfuscatedExternalAccountId": "my_obfuscatedExternalAccountId",
     *   //   "obfuscatedExternalProfileId": "my_obfuscatedExternalProfileId",
     *   //   "orderId": "my_orderId",
     *   //   "paymentState": 0,
     *   //   "priceAmountMicros": "my_priceAmountMicros",
     *   //   "priceChange": {},
     *   //   "priceCurrencyCode": "my_priceCurrencyCode",
     *   //   "profileId": "my_profileId",
     *   //   "profileName": "my_profileName",
     *   //   "promotionCode": "my_promotionCode",
     *   //   "promotionType": 0,
     *   //   "purchaseType": 0,
     *   //   "startTimeMillis": "my_startTimeMillis",
     *   //   "userCancellationTimeMillis": "my_userCancellationTimeMillis"
     *   // }
     * }
    

    【讨论】:

    • 谢谢!我想我最终使用 python 来访问 API,然后我创建了一个 Web 服务,然后我使用 node.js 访问它。这样会简单很多,下次我会用这个方法。我想我这样做是为了验证移动支付。
    【解决方案2】:

    这个呢:https://www.npmjs.com/package/googleapis? 它是 Google 的官方图书馆。

    【讨论】:

    猜你喜欢
    • 2016-04-12
    • 1970-01-01
    • 2019-05-26
    • 2016-05-13
    • 1970-01-01
    • 2017-03-09
    • 1970-01-01
    • 2018-10-20
    • 2010-09-30
    相关资源
    最近更新 更多