GooglePlayValidator - unhashable type: 'slice'
LuptonM opened this issue · 5 comments
First off I have never done IAP receipt validation so have been searching for the least stressful method of implementation.
I believe my key is in the correct format but seem to be having issues
Description
I've read in an api key that is in the same format as the example.
In my example its read in as a dictionary. I've also tried to read it in as a string just in case but still throws an error.
Also running the example throws a bug
Actual Behavior
validator = GooglePlayValidator(bundle_id, api_credentials)
File "C:\Users\Marc\Desktop\Purchase Validation\venv\lib\site-packages\inapppy\googleplay.py", line 45, in init
pem = make_pem(api_key)
File "C:\Users\Marc\Desktop\Purchase Validation\venv\lib\site-packages\inapppy\googleplay.py", line 18, in make_pem
return "\n".join(("-----BEGIN PUBLIC KEY-----", "\n".join(value), "-----END PUBLIC KEY-----"))
File "C:\Users\Marc\Desktop\Purchase Validation\venv\lib\site-packages\inapppy\googleplay.py", line 17, in
value = (public_key[i : i + 64] for i in range(0, len(public_key), 64)) # noqa: E203
TypeError: unhashable type: 'slice'
Possible Fix
Steps to Reproduce
import json
from inapppy import GooglePlayValidator, InAppPyValidationError
bundle_id = 'com.yourcompany.yourapp'
Avoid hard-coding credential data in your code. This is just an example.
with open("service_account.json", "r") as content:
api_credentials = json.load(content)
validator = GooglePlayValidator(bundle_id, api_credentials)
try:
# receipt means androidData
in result of purchase
# signature means signatureAndroid
in result of purchase
validation_result = validator.validate('receipt', 'signature')
except InAppPyValidationError:
# handle validation error
pass
Context
Your Environment
- Version used: 2.5.2
- Operating System and version: Windows 10
I also across to this problem.
Looks like the problem is occur because of wrong example in README.
GooglePlayValidator
doesn't get the credentials file/json file, It requires only Public Key
which has not include "-----BEGIN PUBLIC KEY-----" and "-----END PUBLIC KEY-----" tags.
PS: As you now, you could extract Public Key from Private Key which is accessible in Google service account json.
Same issue
>>> GooglePlayValidator(bundle_id, api_credentials)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/***/python3.8/site-packages/inapppy/googleplay.py", line 45, in __init__
pem = make_pem(api_key)
File "/***/python3.8/site-packages/inapppy/googleplay.py", line 18, in make_pem
return "\n".join(("-----BEGIN PUBLIC KEY-----", "\n".join(value), "-----END PUBLIC KEY-----"))
File "/***/site-packages/inapppy/googleplay.py", line 17, in <genexpr>
value = (public_key[i : i + 64] for i in range(0, len(public_key), 64)) # noqa: E203
TypeError: unhashable type: 'slice'
How to get api_key
?
with something like this:
{
"type": "service_account",
"project_id": "****",
"private_key_id": "*****",
"private_key": "-----BEGIN PRIVATE KEY-----\\*****==\n-----END PRIVATE KEY-----\n",
"client_email": "***.iam.gserviceaccount.com",
"client_id": "****",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/*****.iam.gserviceaccount.com"
}
I tried:
import json
from inapppy import GooglePlayValidator, InAppPyValidationError
from Crypto.PublicKey import RSA
import base64
p = 'credentials.json'
f = open(p)
api_credentials = json.load(f)
private_key = RSA.importKey(api_credentials['private_key'])
public_key = private_key.publickey().exportKey('PEM')
public_key_b64 = base64.standard_b64encode(public_key).decode('ascii')
GooglePlayValidator(bundle_id, public_key_b64)
But i got error:
PyAsn1Error:
Hello,
since the error README is still misleading here is what you are looking for
from inapppy import GooglePlayVerifier, errors
GOOGLE_BUNDLE_ID = 'com.xx.xx'
GOOGLE_SERVICE_ACCOUNT_KEY_FILE = './file.json'
verifier = GooglePlayVerifier(GOOGLE_BUNDLE_ID, GOOGLE_SERVICE_ACCOUNT_KEY_FILE)