send emails from MY gmail account with OAuth2 and

2019-01-28 09:55发布

问题:

I want to send emails from my gmail address through my own server. I'm using nodemailer and using account credentials is flaky, and often times doesn't work and leads to this thread

I've implemented everything on that thread many times, and still it's flaky, and also I know OAuth2 is the way to go.

I have a project with cliendID and clientSecret in google developer console, as you can see:

But how do I get an access token WITHOUT any browser interaction?

I seem to be missing something trivial here ...

I've went through all google tutorials and docs I could find about OAuth2, tokens, and APIs, but all guides go through the browser in one point.

回答1:

Go to the OAuth Playground, click the cog on the top right, check the Use your own OAuth credentials and insert your clientID and clientSecret.

Then select the Gmail API v1 scopes you want in the list to the left and follow the outlined steps and you will get an access_token and a refresh_token.



回答2:

Google Oauth2 actually all Oauth2 implementations I am aware of require that a user grant an application access via a web browser.

There is an alternative type called service accounts this is more like oauth1 service accounts are preauthorized. You can grant a service account access to your google drive by sharing folders and files with the service account like you would any other user. Because they are preauthorized there is no browser window pop up with service accounts.

You can only user service accounts with Gmail if you have a Google domains account Gsuite. The admin can go in and grant the service account access to the Gmail account in question. Perform G Suite Domain-Wide Delegation of Authority

If this is a normal user Gmail account you cant use a service account. You will have to use Oauth2 popup the request and save the refresh token so that you can gain access at a later date.