8

Specifically, I'd like to use the Gmail API to access my own mail only. Is there a way to do this without OAuth and just an API key and/or client id and secret?

Using an API key like:

require('googleapis').gmail('v1').users.messages.list({ auth: '<KEY>', userId: '<EMAIL>') });

yields the following error:

{ errors: 
   [ { domain: 'global',
       reason: 'required',
       message: 'Login Required',
       locationType: 'header',
       location: 'Authorization' } ],
  code: 401,
  message: 'Login Required' }

I suppose that message means they want a valid OAuth "Authorization" header. I would do that but I suppose that's not possible without presenting a webpage.

pinoyyid
  • 20,293
  • 12
  • 57
  • 108
Brandon Zacharie
  • 2,070
  • 2
  • 21
  • 26

1 Answers1

10

The strict answer to "Is there a way to do this without OAuth and just an API key and/or client id and secret?" is no.

However, you can achieve what you are looking for using OAuth. You simply need to store a Refresh Token, which you can then use any time to request an Auth Token to access your gmail.

In order to get the refresh token, you can either write a simple web app to do a one time auth, or follow the steps here How do I authorise an app (web or installed) without user intervention? (canonical ?) which allows you to do the whole auth flow using the Oauth Playground.

Community
  • 1
  • 1
pinoyyid
  • 20,293
  • 12
  • 57
  • 108
  • 2
    The problem is that Google OAuth 2.0 requires you to designate your project as either "Testing" or "Published." OAuth 2.0 tokens issued for "testing" projects are only valid for one week, after which the user must complete the OAuth consent process again. And OAuth 2.0 tokens issued for "published" projects are permanent, but publishing requires submitting your project to Google for review and approval, with a video and a written explanation of your security policy... etc. In short, Google has screwed up its entire service for regular users and the API is functionally unavailable to us. – David Stein Mar 12 '22 at 12:26