Idioms

- Time is precious

Sunday, April 8, 2018

Google/Android Inapp Purchase Validation



1.      Lấy thông tin credentials của ứng dụng
Đầu tiên cần truy cập trang https://console.developers.google.com/ để lấy thông tin credentials của ứng dụng. Nó là một file json có định dạng như sau:
 {
     "installed": {
                 "client_id": "xxx",
                 "project_id": "xxx",
                 "auth_uri": "https://accounts.google.com/o/oauth2/auth",
                 "token_uri": "https://accounts.google.com/o/oauth2/token",
                 "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
                 "client_secret": "xxx",
                 "redirect_uris": [
                             "urn:ietf:wg:oauth:2.0:oob",
                             "http://localhost"
                 ]
     }
}
2.      Lấy refresh token
2.1. Lấy refresh token tạm
Thực hiện GET url: https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/androidpublisher&response_type=code&access_type=offline&redirect_uri=...&client_id=...  
     Với redirect_uri và client_id đã có trong phần 1.
     Kết quả nhận được là một token có dạng: 4/AADR895tjYDHghLFe0uj4NJ7lvxaHd4g2GyCLLp_oYTP4eXSyByhSuI
2.2. Lấy refresh token
Thực hiện POST url: https://accounts.google.com/o/oauth2/token
Với các params
grant_type=authorization_code
code=<the code from the previous step>
client_id=<the client ID token created in the APIs Console>
client_secret=<the client secret corresponding to the client ID>
redirect_uri=<the URI registered with the client ID>3. Validation
Kết quả trả về
{
    "access_token": "ya29.GluXBSbYUJAenC2hkBF12vbDfDUdoLnvNpmjZMzH4QPEm45FmTKvsBPfd_g9nphk0GnjvziY-M6iWIoeZ-XtNDtRU3mHDGcApF8mjn_VZFJDg11EpIFNvjtKBMkF",
    "expires_in": 3600,
    "refresh_token": "1/4Dsy45rUTS1TSX-EHBjwzGZwnWRzV91fkVJHiYQKRWQKN8Rm1f0r5EFHIGQwG_Fc",
    "token_type": "Bearer"
}
3.     Validation
Cần truyền các params:
packageName, productId, token, access_token
Kết quả trả về
{
    "kind": "androidpublisher#productPurchase",
    "purchaseTimeMillis": "1523007344208",
    "purchaseState": 1,
    "consumptionState": 1,
    "developerPayload": "",
    "orderId": "GPA.3394-2497-6368-06851",
    "purchaseType": 0
}
4.     Tham khảo

0 comments:

Post a Comment