gmailgmail-api

GMail API "Precondition check failed." when trying to list messages


The Problem

I'm trying to use the GMail API via the Offical Google .NET Libraries. I'm building a ASP .NET 8 App with razor pages. I manage to sign the user in and store the token in the database. I can also retrieve the Token from the Database and get a new access token based on my stored refresh token. However, when I try to retrieve the messages I get a "BAD Request" response:

{
  "error": {
    "code": 400,
    "message": "Precondition check failed.",
    "errors": [
      {
        "message": "Precondition check failed.",
        "domain": "global",
        "reason": "failedPrecondition"
      }
    ],
    "status": "FAILED_PRECONDITION"
  }
}

What I've tried

First I request a new Access Token:

https://oauth2.googleapis.com/token
User-Agent = google-api-dotnet-client/1.68.0.0; User-Agent = (gzip)
refresh_token=***&grant_type=refresh_token&client_id=***.apps.googleusercontent.com&client_secret=***

Which succeeds:

{
  "access_token": "***",
  "expires_in": 3599,
  "scope": "https://www.googleapis.com/auth/gmail.readonly https://www.googleapis.com/auth/userinfo.profile openid https://www.googleapis.com/auth/userinfo.email",
  "token_type": "Bearer",
  "id_token": "***"
}

Afterwards I try to request the messages:

https://gmail.googleapis.com/gmail/v1/users/me/messages
User-Agent = ConsographKengraAdapterGoogle; User-Agent = google-api-dotnet-client/1.68.0.0; User-Agent = (gzip); x-goog-api-client = gl-dotnet/8.0.0 gdcl/1.68.0.3399; Authorization = Bearer ***

Which fails with the aforementioned error response:

{
  "error": {
    "code": 400,
    "message": "Precondition check failed.",
    "errors": [
      {
        "message": "Precondition check failed.",
        "domain": "global",
        "reason": "failedPrecondition"
      }
    ],
    "status": "FAILED_PRECONDITION"
  }
}

I'm not sure what could be the issue.


Solution

  • In my case I was using a freshly created google account for testing. Turns out, you have to activly add a gmail account to your google account which wasn't always the case.

    After adding gmail to my google account it worked flawlessly.