0

I am trying to set priority for an app-update using Google Play Developer Publishing API. As per documentation,I need to call androidpublisher.googleapis.com services. I installed Cloud SDK in my Linux device to make service call.Also i have performed all steps and able to initialize sdk using gcloud init.

But when i try to perform Edits using below command

curl -X POST -H "Authorization: Bearer [oauth2_client_id]" -H "Content-Type: application/json" https://androidpublisher.googleapis.com/androidpublisher/v3/applications/com.xyz.abc/edits -d'{"id": "20","expiryTimeSeconds": "180}'

I get below error-

{ "error": { "code": 401, "message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.", "status": "UNAUTHENTICATED" } }

I have already provided oauth client id in request!How to resolve this?If OAuth 2 access token is needed , how will i get that using gcloud?

UPDATE

Based on answer i sent token inside authorization and got error-

{
  "error": {
    "code": 403,
    "message": "Request had insufficient authentication scopes.",
    "status": "PERMISSION_DENIED"
  }
}

why i am still getting error?

UPDATE2

Now I am able to get code/token (not sure if it is access token) with required scopes using following URL- https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/androidpublisher&response_type=code&access_type=offline&redirect_uri={your redirect url}&client_id={your client ID})

Below are the details of steps i performed but couldn't make it work-

1.Open Terminal and move to google cloud sdk bin folder using command- cd /home/bhuvnesh.varma/Downloads/google-cloud-sdk/bin

2.Run command - gcloud init After selecting configuration,account and project i get message- Your Google Cloud SDK is configured and ready to use!

3.I hit this URL on browser replacing client id and redirect url- https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/androidpublisher&response_type=code&access_type=offline&redirect_uri={your redirect url}&client_id={your client ID})

I got the message-

"Please copy this code, switch to your application and paste it there: 4/AX4XfWgUvVTE0VknkW0R35rUOsNZ7IVmMCebiV3yyyKxP4c7y0jjZxxcF0TQg"

4.Then i performed Edits command on terminal using generated token/code-

curl -X POST -H "Authorization: Bearer 4/AX4XfWgUvVTE0VknkW0R35rUOsNZ7IVmMCebiV3yyyKxP4c7y0jjZxxcF0TQg" -H "Content-Type: application/json" https://androidpublisher.googleapis.com/androidpublisher/v3/applications/com.xyz.abc/edits -d'{"id": "20","expiryTimeSeconds": "180}'

Still i received below error- { "error": { "code": 401, "message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.", "status": "UNAUTHENTICATED" } }

Android Developer
  • 9,157
  • 18
  • 82
  • 139

1 Answers1

1

The Bearer value should be an OAuth access token.

I'm unfamiliar with this API. Is it operating on behalf of a specific (individual) user? Corollary: does it require user-specific information? If so, you should be able to authenticate using the account that you used to gcloud auth login. You can get an access token for it using:

TOKEN=$(gcloud auth print-access-token)

curl ... --header "Authorization: Bearer ${TOKEN}" ...

If not (that doesn't work) you should create a service account to authenticate the call. This process is more involved as you'll also need to create a key for that account and authenticate using it with gcloud auth activate-service-account. You can then resume the gcloud auth print-access-token above.

NOTE It is almost always best practice to use Google's client SDKs for interacting with its services. Using these, auth is handled -- mostly transparently -- for you.

DazWilkin
  • 32,823
  • 5
  • 47
  • 88
  • Hi.. it's not user specific api but it's developer api which is used to set priority for an app update which is used in force or flexible upgrade dialog shown to users using older version of app – Android Developer Jun 29 '21 at 15:45
  • When using gcloud I am already logged in through my developer account.then it looks odd that I have to call gcloud auth login to again login and I am getting UNAUTHENTICAted error.. however I'll try! – Android Developer Jun 29 '21 at 15:48
  • As per [this](https://cloud.google.com/apis/docs/client-libraries-explained)`If you don't want to access an API programmatically, you can access some of the same functionality using the tools in Cloud SDK or using the Google Cloud Console`. And i am using Cloud SDK.So,why Google's client SDKs? – Android Developer Jun 29 '21 at 16:01
  • Thanks.I tried your way of applying token and got this error this time-` "error": { "code": 403, "message": "Request had insufficient authentication scopes.", "status": "PERMISSION_DENIED" } }` – Android Developer Jun 29 '21 at 16:05
  • Can you call the method successfully using APIs Explorer? https://developers.google.com/android-publisher/api-ref/rest/v3/edits/insert – DazWilkin Jun 29 '21 at 16:44
  • You'd only need to `gcloud auth` again if you're unable to use your user account (and must authenticate as a service account). Different credentials require that these must be present in your local config by having `gcloud auth ...` at least once (and not been revoked). – DazWilkin Jun 29 '21 at 16:46
  • Google's SDKs use the underlying REST API. So, there's no functional difference. They just make life easier (you probably would not be facing this issue). But, if you'd like to stick with `curl`, we'll get it working. – DazWilkin Jun 29 '21 at 16:47
  • I'm unsure whether|how you can change the OAuth scopes using `gcloud auth print-access-token`. Google provides [`oauth2l`](https://github.com/google/oauth2l) and this has a wrapper for `curl`, [`oauth2l curl ...`](https://github.com/google/oauth2l#curl) that can get an access token with revised scopes. You will need [`androidpublisher`](https://developers.google.com/android-publisher/api-ref/rest/v3/edits/insert#authorization-scopes) – DazWilkin Jun 29 '21 at 16:56
  • Please check Update2 in question! – Android Developer Jun 30 '21 at 16:26
  • That looks like an authorization token (`4/...`) not an access token. Visit Google's [OAuth Playground](https://developers.google.com/oauthplayground), either select the API or input the scope directly, then click "Exchange authorization code for tokens" and use the `access_token` it provides as the bearer token in the `curl` request. You can validate the `access_token` by pasting it into the URL: `https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=[[access_token]]` – DazWilkin Jun 30 '21 at 17:01
  • I am getting this error now `The project id used to call the Google Play Developer API has not been linked in the Google Play Developer Console.` – Android Developer Jun 30 '21 at 18:54
  • Please consider Googling the issue or submitting another question. I've provided guidance on the auth issue raised in this question. – DazWilkin Jun 30 '21 at 20:04