Example response of API call
https://www.postman.com/collections/6655f0c79fe567dd58fc
Other reading
https://www.labnol.org/google-api-service-account-220405
https://medium.com/@sirajul.anik/understanding-oauth-2-0-179543b18bd
Original packages from google API doc (more complicated solutions)
https://www.npmjs.com/package/googleapis
https://www.npmjs.com/package/@google-cloud/local-auth
Packages that have simple usages than google API docs
https://github.com/SpiderStrategies/node-gmail-api
https://github.com/jaredhanson/passport-google-oauth
https://oauth.net/2/
Important flows
https://developers.google.com/identity/protocols/oauth2
https://developers.google.com/gmail/api/guides
https://developers.google.com/gmail/api/quickstart/nodejs
To get an access token using OAuth 2.0 Client ID (Desktop application)
scopes
into Gmail API scope
https://www.daimto.com/how-to-get-a-google-access-token-with-curl/
GET
Query string | Value |
---|---|
access_type | offline |
scope | https://mail.google.com/ |
client_id | The created OAuth 2.0 Client ID client id |
redirect_uri | urn:ietf:wg:oauth:2.0:oob |
response_type | code |
For viewing only, since the get code steps need manual work in the browser
Use authorization code, only need to run this step at first time, can store and reuse the refresh token.
POST
Query string | Value |
---|---|
code | The Authorization code from step 1. Get Authorization Code |
client_id | The created OAuth 2.0 Client ID client id |
client_secret | The created OAuth 2.0 Client ID client secret |
redirect_uri | urn:ietf:wg:oauth:2.0:oob |
grant_type | authorization_code |
POST
Query string | Value |
---|---|
refresh_token | The refresh token from step 2. Get refresh token and access token |
client_id | The created OAuth 2.0 Client ID client id |
client_secret | The created OAuth 2.0 Client ID client secret |
redirect_uri | urn:ietf:wg:oauth:2.0:oob |
grant_type | refresh_token |
Use the access token from the response to access Gmail API
https://mailtrap.io/blog/send-emails-with-gmail-api/
GET
user_id |
---|
The testing user gmail that is saved in OAuth consent screen, for example, abc@gmail.com |
Set the Authorization type of the request to Bearer Token (which is the access token from step 3. Get access token using the refresh token)
Solution
https://stackoverflow.com/questions/73062345/error-403-access-denied-when-usign-google-oauth
Add a testing user gmail and save (which will be the test account that accesses the Gmail API), for example, abc@gmail.com
After adding the testing user, and choosing the authorization account again, click Continue after seeing:
And keep clicking Continue
Finally copy and save the Authorization Code that shows in the browser
Solution
Do step 1. Get Authorization Code again to get a new Authorization Code, and use the new Authorization Code to get the refresh token by doing step 2. Get refresh token and access token.
api testing
test automation
software test