Invalid scope, can't get Gmail SMTP token

Issue: OAuth2 Access Token not Being Generated for Gmail

I am trying to use an SMTP library to send mail via Gmail. To do this, I need to generate an OAuth2 access token. I have set up an OAuth2 client account in the Google API console, enabled the Gmail API, and added the https://mail.google.com/ scope in the OAuth consent screen setup. However, when I make a POST request to https://accounts.google.com/o/oauth2/device/code, I get the error invalid_scope. When I switch the scope to email, the request goes through. I have tested this with two different HTTP clients to make sure this wasn’t due to some encoding issue.

What am I doing wrong?

The issue is that you are using the incorrect scope for generating an OAuth2 access token for Gmail. Instead of using the https://mail.google.com/ scope, you should use the https://www.googleapis.com/auth/gmail.send scope. This should allow you to generate an access token that can be used to send mail via Gmail using SMTP.