Created
March 15, 2017 09:52
-
-
Save jeffhuangtw/4b0c1d9ceff280c85505fe84f1dc9020 to your computer and use it in GitHub Desktop.
[nodejs] server side check "androidpublisher.purchases.subscriptions.get" with "service account"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 = '[email protected]'; | |
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