Search code examples
httpgoogle-apigmailgmail-api

Gmail search with API and access token


I am trying to fetch Gmail search results from Gmail API using an access token.

The following code works and returns an array of my email IDs:

fetch(`https://www.googleapis.com/gmail/v1/users/${userId}/messages?access_token=${accessToken}`)

I then try to append a search query following the Gmail API documentation guidelines

fetch(`https://www.googleapis.com/gmail/v1/users/${userId}/messages?access_token=${accessToken}?q=${text}`)

and it brings this error code: 401, message: 'Invalid Credentials'

Authentication scope is set to https://mail.google.com/ which assumes full control of the email. I tried swapping access_token and q parameters, as well as removing the access_token parameter but still no success. What am I doing wrong?


Solution

  • `https://www.googleapis.com/gmail/v1/users/${userId}/messages?access_token=${accessToken}?q=${text}

    you are preforming a HTTP GET in this call. Additional parameters are tacked on using a & only the first one starts with a ?

    try this:

    `https://www.googleapis.com/gmail/v1/users/${userId}/messages?access_token=${accessToken}&q=${text}