0

I am using userEvents.write method with from my web app. I sent request a JSON like below with Authorization header got from gcloud auth application-default print-access-token command.

{
  "eventType": "detail-page-view",
  "visitorId": "xxxxxxxxxx.xxxxxxxxxxxx",
  "eventTime": "2021-03-06T09:13:58+09:00",
  "experimentIds": [
    "Gie-xxxxxxxxxxxxxxxxxxxxxxxx"
  ],
  "userInfo": {
    "userId": "",
    "ipAddress": "xxx.xxx.xxx.xxx",
    "userAgent": "Mozilla/5.0 (Linux; Android 11; Pixel 4a (5G)) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.181 Mobile Safari/537.36",
    "directUserRequest": false
  },
  "uri": "https://xxxxx.net/aaaaaa/item/196160",
  "referrerUri": "https://xxxxxxxx.net/item/196160",
  "productDetails": [
    {
      "product": {
        "id": "196160"
      }
    }
  ]
}

Request url is like this. https://retail.googleapis.com/v2/projects/{PROJECT_NUMBER}/locations/global/catalogs/default_catalog/userEvents:write

In most case it goes well. But, sometime returns error below.

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

This error becomes not to appear after the app restart and it occures again after few days. Why this error occures? How can I fix it?

1 Answers1

0

I believe that Application Default Credentials which is used for the authentication method is not getting the correct credentials while you're running the app.

It could be that the reason about why is not working is because Access tokens have limited lifetimes. As you said the error appears after some time so, the most probably explanation is that your token had expired and you need to refresh it.

If your application needs access to a Google API beyond the lifetime of a single access token, it can obtain a refresh token. A refresh token allows your application to obtain new access tokens.

Check the official documentation in order to Obtain OAuth 2.0 Access tokens and how to configure it for different languages.

Sergio NH
  • 185
  • 5