Search code examples
oauthgoogle-apigoogle-oauthgoogle-play-servicesservice-accounts

Google play apis - purchases.products.get for purchase validation gives 401 OAuth2 error


I aim to fetch purchases from googleapis by using a service account on server-side.

Method: purchases.products.get api requires an oauth2 authentication.

Therefore I create an oauth2 token -from the client-secret.json I am provided from consolce.cloud.google- inside of my server-side backend java spring application.

    GoogleCredentials googleCredentials = GoogleCredentials.fromStream(new FileInputStream("src/main/resources/client_secret.json")).createScoped("https://www.googleapis.com/auth/androidpublisher");
    googleCredentials.refresh();
    AccessToken accessToken = googleCredentials.getAccessToken();

The token I generate is like 'ya29.c.b0AXczHcuLszNI................' It ends with multiple dots I don't know why.

https://androidpublisher.googleapis.com/androidpublisher/v3/applications/{packageName}/purchases/products/{productId}/tokens/{token}

After I get the token I do the GET request to this URL. I gets the following error:

"message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",

enter image description here

Why my token is not working? The way I use it, Is it wrong? And/or oauth2 token generation way is it wrong?


Solution

  • The access token is sent as an authorization header. The access token should be prefexed with the term bearer as it is a bearer token.