What is the best way to access Google Play Developers API with Node.js?
Asked Answered
J

2

5

There are only Python and Java client wrappers for android-publisher API (used to verify IAP). With Node.js it seems like the only way is via http however, I was wondering if there is a way to use the Google API Node.js client to generate the authorization token for OAuth service-to-service authentication and pass it to the http call. Generating the JWT token by hand is an arduous and lengthy process. Is there a best practice / recommended way for doing this the easy way?

Jeromejeromy answered 27/5, 2020 at 22:40 Comment(0)
M
11

Eldermao, just use google.auth.GoogleAuth to get the JWT client for authenticated with Google API. Here is the example from the google code :

// 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"
  // }
}
Marquand answered 7/10, 2020 at 19:30 Comment(2)
Thanks! I think I ended up using python to access the API and then I created a web service which I then access using node. In this way it would be much more straightforward, and I will use this approach next time. I think I was doing this to verify mobile payments.Jeromejeromy
If you use service account here is the additional parameter to pass in ``` const auth = new docs.auth.GoogleAuth({ keyFilename: 'PATH_TO_SERVICE_ACCOUNT_KEY.json', // Scopes can be specified either as an array or as a single, space-delimited string. scopes: ['googleapis.com/auth/documents'] }); ```Whittier
M
0

what about this one : https://www.npmjs.com/package/googleapis ? It is the official library by Google.

Marquand answered 27/9, 2020 at 19:32 Comment(3)
Generally, links to a tool or library should be accompanied by usage notes, a specific explanation of how the linked resource is applicable to the problem, or some sample code, or if possible all of the above.Autodidact
The library you suggest does not list the android-publisher API as part of their supported APIs.Jeromejeromy
at the line 9500 here : github.com/googleapis/google-api-nodejs-client/blob/master/src/…, you can find an example of how to use the android-publisher apiMarquand

© 2022 - 2024 — McMap. All rights reserved.