Skip to content

Commit 1cfa923

Browse files
updated docs
1 parent 9362f08 commit 1cfa923

File tree

1 file changed

+149
-3
lines changed

1 file changed

+149
-3
lines changed

README.rst

Lines changed: 149 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ Table of contents
1717
2. Installation
1818
3. Google Play (`receipt` + `signature`)
1919
4. Google Play (verification)
20+
21+
- Setting up Google Service Account Credentials
22+
- Usage Example (with file path)
23+
- Usage Example (with credentials dictionary)
24+
2025
5. Google Play (verification with result)
2126
6. Google Play (consuming products)
2227
7. App Store (`receipt` + using optional `shared-secret`)
@@ -94,6 +99,95 @@ An additional example showing how to authenticate using dict credentials instead
9499
95100
4. Google Play verification
96101
===========================
102+
103+
Setting up Google Service Account Credentials
104+
----------------------------------------------
105+
106+
Before using Google Play verification, you need to set up a Google Service Account and obtain the credentials file. This section explains what ``GOOGLE_SERVICE_ACCOUNT_KEY_FILE`` is and how to obtain it.
107+
108+
**What is GOOGLE_SERVICE_ACCOUNT_KEY_FILE?**
109+
110+
``GOOGLE_SERVICE_ACCOUNT_KEY_FILE`` is a JSON file containing a service account's private key and credentials. This file authorizes your application to access the Google Play Developer API to verify in-app purchases and subscriptions.
111+
112+
The credentials can be provided in two ways:
113+
114+
1. **As a file path** (string): Path to the JSON key file downloaded from Google Cloud Console
115+
2. **As a dictionary** (dict): The parsed JSON content of the key file
116+
117+
**How to obtain the Service Account Key File:**
118+
119+
1. **Link Google Cloud Project to Google Play Console**
120+
121+
- Go to `Google Play Console <https://play.google.com/console>`_
122+
- Select your app
123+
- Navigate to **Settings → Developer account → API access**
124+
- If you haven't linked a project yet, click **Link** to create or link a Google Cloud project
125+
- Accept the terms and conditions
126+
127+
2. **Create a Service Account**
128+
129+
- In the API access page, scroll to **Service accounts**
130+
- Click **Create new service account** or **Learn how to create service accounts** (this will take you to Google Cloud Console)
131+
- In Google Cloud Console:
132+
133+
- Go to **IAM & Admin → Service Accounts**
134+
- Click **+ CREATE SERVICE ACCOUNT**
135+
- Enter a name (e.g., "InAppPy Validator") and description
136+
- Click **CREATE AND CONTINUE**
137+
- Skip granting roles (not needed for this step)
138+
- Click **DONE**
139+
140+
3. **Grant Permissions in Google Play Console**
141+
142+
- Return to Google Play Console → **Settings → Developer account → API access**
143+
- Find your newly created service account in the list
144+
- Click **Grant access**
145+
- Under **App permissions**, select your app
146+
- Under **Account permissions**, enable:
147+
148+
- **View financial data** (for viewing purchase/subscription info)
149+
- **Manage orders and subscriptions** (if you need to consume products or manage subscriptions)
150+
151+
- Click **Invite user** and then **Send invitation**
152+
153+
4. **Download the JSON Key File**
154+
155+
- Go back to **Google Cloud Console → IAM & Admin → Service Accounts**
156+
- Click on your service account email
157+
- Go to the **KEYS** tab
158+
- Click **ADD KEY → Create new key**
159+
- Select **JSON** as the key type
160+
- Click **CREATE**
161+
- The JSON key file will be automatically downloaded
162+
- **IMPORTANT**: Store this file securely! It contains a private key and cannot be recovered if lost
163+
164+
5. **Important Notes**
165+
166+
- The JSON key file should contain fields like: ``type``, ``project_id``, ``private_key_id``, ``private_key``, ``client_email``, etc.
167+
- Keep this file secure and never commit it to version control
168+
- In some cases, you may need to create at least one product in your Google Play Console before the API access works properly
169+
- It may take a few minutes for permissions to propagate after granting access
170+
171+
**Example JSON key file structure:**
172+
173+
.. code:: json
174+
175+
{
176+
"type": "service_account",
177+
"project_id": "your-project-id",
178+
"private_key_id": "a1b2c3d4e5f6...",
179+
"private_key": "-----BEGIN PRIVATE KEY-----\nYourPrivateKeyHere\n-----END PRIVATE KEY-----\n",
180+
"client_email": "[email protected]",
181+
"client_id": "123456789",
182+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
183+
"token_uri": "https://oauth2.googleapis.com/token",
184+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
185+
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/..."
186+
}
187+
188+
Usage Example (with file path)
189+
-------------------------------
190+
97191
.. code:: python
98192
99193
from inapppy import GooglePlayVerifier, errors
@@ -105,9 +199,53 @@ An additional example showing how to authenticate using dict credentials instead
105199
"""
106200
purchase_token = receipt['purchaseToken']
107201
product_sku = receipt['productId']
202+
203+
# Pass the path to your service account JSON key file
204+
verifier = GooglePlayVerifier(
205+
GOOGLE_BUNDLE_ID,
206+
'/path/to/your-service-account-key.json', # Path to the JSON key file
207+
)
208+
response = {'valid': False, 'transactions': []}
209+
try:
210+
result = verifier.verify(
211+
purchase_token,
212+
product_sku,
213+
is_subscription=True
214+
)
215+
response['valid'] = True
216+
response['transactions'].append(
217+
(result['orderId'], product_sku)
218+
)
219+
except errors.GoogleError as exc:
220+
logging.error('Purchase validation failed {}'.format(exc))
221+
return response
222+
223+
224+
Usage Example (with credentials dictionary)
225+
--------------------------------------------
226+
227+
.. code:: python
228+
229+
import json
230+
from inapppy import GooglePlayVerifier, errors
231+
232+
233+
def google_validator(receipt):
234+
"""
235+
Accepts receipt, validates in Google using dict credentials.
236+
"""
237+
purchase_token = receipt['purchaseToken']
238+
product_sku = receipt['productId']
239+
240+
# Load credentials from environment variable or secure storage
241+
# NEVER hard-code credentials in your source code!
242+
credentials_json = os.environ.get('GOOGLE_SERVICE_ACCOUNT_JSON')
243+
credentials_dict = json.loads(credentials_json)
244+
245+
# Pass the credentials as a dictionary
108246
verifier = GooglePlayVerifier(
109247
GOOGLE_BUNDLE_ID,
110-
GOOGLE_SERVICE_ACCOUNT_KEY_FILE,
248+
credentials_dict, # Dictionary containing the JSON key data
111249
)
112250
response = {'valid': False, 'transactions': []}
113251
try:
@@ -129,6 +267,8 @@ An additional example showing how to authenticate using dict credentials instead
129267
=========================================
130268
Alternative to `.verify` method, instead of raising an error result class will be returned.
131269

270+
**Note:** See section 4 for instructions on setting up ``GOOGLE_SERVICE_ACCOUNT_KEY_FILE``.
271+
132272
.. code:: python
133273
134274
from inapppy import GooglePlayVerifier, errors
@@ -140,9 +280,11 @@ Alternative to `.verify` method, instead of raising an error result class will b
140280
"""
141281
purchase_token = receipt['purchaseToken']
142282
product_sku = receipt['productId']
283+
284+
# Use the service account credentials (see section 4 for setup)
143285
verifier = GooglePlayVerifier(
144286
GOOGLE_BUNDLE_ID,
145-
GOOGLE_SERVICE_ACCOUNT_KEY_FILE,
287+
GOOGLE_SERVICE_ACCOUNT_KEY_FILE, # Path to JSON key file or dict
146288
)
147289
response = {'valid': False, 'transactions': []}
148290
@@ -165,6 +307,8 @@ Alternative to `.verify` method, instead of raising an error result class will b
165307
After validating a purchase, you can consume a one-time product to prevent refunds and allow the user to purchase it again.
166308
This is a separate operation that should be called after verification to handle cases like power outages between validation and consumption.
167309

310+
**Note:** See section 4 for instructions on setting up ``GOOGLE_SERVICE_ACCOUNT_KEY_FILE``.
311+
168312
.. code:: python
169313
170314
from inapppy import GooglePlayVerifier, errors
@@ -176,9 +320,11 @@ This is a separate operation that should be called after verification to handle
176320
"""
177321
purchase_token = receipt['purchaseToken']
178322
product_sku = receipt['productId']
323+
324+
# Use the service account credentials (see section 4 for setup)
179325
verifier = GooglePlayVerifier(
180326
GOOGLE_BUNDLE_ID,
181-
GOOGLE_SERVICE_ACCOUNT_KEY_FILE,
327+
GOOGLE_SERVICE_ACCOUNT_KEY_FILE, # Path to JSON key file or dict
182328
)
183329
184330
try:

0 commit comments

Comments
 (0)