I keep getting the following error when I execute my code:
An error occurred:
Even though the accepted answer is 100% correct. I think it's worth pointing out that why that's the case.
When you authorize a gmail service client, you can specify several different scopes: All, compose, labels, etc...
These are all listed here: https://developers.google.com/gmail/api/auth/scopes
The scope mentioned in the answer provides complete gmail access.
If you run the official "gmail-python-quickstart" before, please delete the file "gmail-quickstart.json" in your system. Rerun your program again so that you can set the priviledge as you want.
Gmail API has these scopes:
For sending emails, https://www.googleapis.com/auth/gmail.send is needed or full access https://mail.google.com/.
The scopes taken from here.
If you used google's official example, there should be a folder in ~/.credentials/
directory which is old, remove everything inside that directory and re-run your code. then you have to add new permissions and then everything is OK!
In Addition to the answers from:
and as a furtherance of ccy's answer...
If you're using the original gmail-python-quickstart
code, make sure to also update the following:
'/path/to/your/secret_client.json'
get_credentials()
to use the failed credentials logic path...if True:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
if flags:
credentials = tools.run_flow(flow, store, flags)
else: # Needed only for compatibility with Python 2.6
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
to force True
so that the logic operation will definitely work the client.flow
operations:
if True:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
if flags:
credentials = tools.run_flow(flow, store, flags)
else: # Needed only for compatibility with Python 2.6
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
This is a hacky fix, but will get you up and going in short time.
flow
code, which opens up the authentication window browser and requires that the end user accept the security protocol before sending the email.I found that doing the following works:
secret-client-####.html.json
file to the directory defined in the first block of code from the get_credentials()
method. Basically, copy it to your user/.credentials
directorygmail-python-quickstart.json
gmail-python-quickstart.json
Run your code, and then it should work fine.
Solved it by changing the SCOPES line to:
SCOPES = 'https://mail.google.com/'
Email sending works perfectly