Search code examples
androidgoogle-playin-app-billinggoogle-developers-consoleplay-billing-library

How do i generate refresh and access token for Google Play Developers API


My goal is to validate user purchases on google server as described here > Purchases.products: get

but i need to authorise the request > Authorisation Documentation

According to Google Play Developer API Authorization Doccumentation in order to generate access and refresh token :

"... sending a POST request to https://accounts.google.com/o/oauth2/token with the following fields set:

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>

A successful response will contain your tokens in JSON format:

{
  "access_token" : "ya29.ZStBkRnGyZ2mUYOLgls7QVBxOg82XhBCFo8UIT5gM",
  "token_type" : "Bearer",
  "expires_in" : 3600,
  "refresh_token" : "1/zaaHNytlC3SEBX7F2cfrHcqJEa3KoAHYeXES6nmho"
}

"

i successfully generated code, client_id, client_secret, redirect_uri from console.developers.google.com but when i send the POST request

https://accounts.google.com/o/oauth2/token?grant_type=authorization_code&code=my_generated_codeA&client_id=generated_client_id&client_secret=generated_client_secret&redirect_uri=my_redirect_uri

i get the the following response when i used Postman:

{
    "error": "invalid_request",
    "error_description": "Missing header: Content-Type"
}

with status code = 400

i get the the following response when i used Chrome :

{
    "error": "invalid_request"
}

How can i get the right response?


Solution

  • The https://accounts.google.com/o/oauth2/token?grant_type=authorization_code&code=my_generated_codeA&client_id=generated_client_id&client_secret=generated_client_secret&redirect_uri=my_redirect_uri is GET request, it's not POST request because there is no request body.

    Also, when using Postman the response

    {
    "error": "invalid_request",
    "error_description": "Missing header: Content-Type"
    }
    

    means that you select the wrong header. You should select application/x-www-form-urlencoded option in Bodytab in Postman. Then write down key pair value. You will get something like this:

    enter image description here