Skip to content

Instantly share code, notes, and snippets.

@jeffhuangtw
Created March 15, 2017 09:52
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save jeffhuangtw/4b0c1d9ceff280c85505fe84f1dc9020 to your computer and use it in GitHub Desktop.
Save jeffhuangtw/4b0c1d9ceff280c85505fe84f1dc9020 to your computer and use it in GitHub Desktop.
[nodejs] server side check "androidpublisher.purchases.subscriptions.get" with "service account"
// Google Play API Key
// ref: http://stackoverflow.com/questions/35127086/android-inapp-purchase-receipt-validation-google-play
// ref: https://developers.google.com/android-publisher/authorization
// ref: http://google.github.io/google-api-nodejs-client/18.0.0/index.html#toc14__anchor
//
// install npm package
// ref: https://github.com/google/google-api-nodejs-client
// $ npm install googleapis --save
//
const google = require('googleapis');
const publisher = google.androidpublisher('v2');
const OAuth2 = google.auth.OAuth2;
const SERVICE_ACCOUNT_EMAIL = 'your_service_account_email@developer.gserviceaccount.com';
const SERVICE_ACCOUNT_KEY_FILE = require('./path/to/your/service_account.json');
const jwtClient = new google.auth.JWT(
SERVICE_ACCOUNT_EMAIL,
null,
SERVICE_ACCOUNT_KEY_FILE.private_key,
['https://www.googleapis.com/auth/androidpublisher'],
null
);
// setup auth method with service account
google.options({ auth: jwtClient });
// get subscription status by purchaseToken
publisher.purchases.subscriptions.get({
packageName: 'The package name of the android app',
subscriptionId: 'The purchased subscription ID',
token: 'The purchaseToken of user'
}, function (error, response) {
if (error) {
console.log("verify google subscriptions error:" + JSON.stringify(error));
return handleSubscriptionsError(error);
}
return handleUpdateUserExpiration(response);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment