关于获取应用内购买令牌及从Google Play Console获取令牌用于取消订阅的技术咨询
获取应用内购买令牌及从Google Play Console获取令牌用于取消订阅的技术咨询
Hey there! Great question—there are actually a few reliable ways to get purchase tokens for canceling subscriptions and stopping auto-renewal, both directly through the Play Console and programmatically. Let me break them down for you:
1. Directly Export Subscription Data from Google Play Console
This is the simplest method for bulk retrieval, no coding required:
- Log into your Google Play Console and navigate to your target app.
- Go to Monetization > Subscriptions (or Orders > All orders if you need individual transaction tokens).
- Look for the Export button at the top of the page. Choose your desired time range and export format (CSV or JSON).
- The exported file will include the
purchaseTokenfield for every subscription/order—this is exactly the token you need to cancel auto-renewal.
2. Use the Google Play Developer API for Automated/Bulk Access
If you need to automate this process or integrate it into your backend systems, the API is the way to go:
- First, enable the Google Play Developer API in your Play Console’s API access section, and create a service account with the appropriate credentials (make sure it has permissions like "View app data" or "Edit app data").
- Use the
purchases.subscriptions.list(orpurchases.subscriptionsv2.listfor newer subscription models) endpoint. You’ll need to provide your app’s package name and the user’s Google ID (if you have it stored in your database). - Here’s a quick Python example of how to call the API:
from googleapiclient.discovery import build from google.oauth2 import service_account # Load your service account credentials credentials = service_account.Credentials.from_service_account_file( 'path/to/your/credentials.json', scopes=['https://www.googleapis.com/auth/androidpublisher'] ) # Initialize the Android Publisher service service = build('androidpublisher', 'v3', credentials=credentials) # Fetch subscriptions for a specific user subscriptions_response = service.purchases().subscriptions().list( packageName='com.your.app.package', userId='user_google_account_id' ).execute() # Extract purchase tokens from the response purchase_tokens = [sub['purchaseToken'] for sub in subscriptions_response.get('subscriptions', [])] - Note: To get tokens for all users, you’ll need to pair this with your own user database (since the API requires a user ID to query their subscriptions).
3. Retrieve Tokens In-App for Individual Users
If you’re handling a single user’s cancellation request, you can fetch their token directly from your app using the Google Play Billing Library:
- Integrate the latest Google Play Billing Library (v5+ is recommended) into your app.
- Use the
queryPurchasesAsyncmethod to fetch the user’s active subscriptions. Each returnedPurchaseobject has agetPurchaseToken()method that gives you the token. - You can then send this token to your backend to process the cancellation, or call the cancellation API directly from your app (though backend processing is safer).
Best Practices to Keep in Mind
- Store tokens in your backend: Whenever a user subscribes, save their
purchaseTokento your database. This eliminates the need to fetch it from Play Console every time you need to manage their subscription. - Batch operations efficiently: For bulk cancellations, use the Play Console export or API batch requests instead of manual lookup—it saves time and reduces errors.
- Validate permissions: When using the API, double-check that your service account has the correct permissions to access subscription data and modify subscriptions.
- Confirm cancellation: After using the token to call the
purchases.subscriptions.cancelendpoint, verify that the subscription’s auto-renewal status is set tofalseto ensure the operation worked.
内容来源于stack exchange
相关产品推荐
相关产品推荐

