When I run the code with body() I will get the Nonetype error.
I've tried putting the body into batchDelete() parameter but it causes delegation denied. I have given access to the Scope in cloud.google.com and at this point I'm not sure what I can do next.
#pylint:disable=E1101
from __future__ import print_function
import os.path
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
# If modifying these scopes, delete the file token.json.
SCOPES = ['https://mail.google.com/']
# The ID of a sample document.
DOCUMENT_ID = '195j9eDD3ccgjQRttHhJPymLJUCOUjs-jmwTrekvdjFE'
def main():
"""Shows basic usage of the Docs API.
Prints the title of a sample document.
"""
creds = None
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.json', 'w') as token:
token.write(creds.to_json())
try:
service = build('gmail', 'v1', credentials=creds)
# Retrieve the documents contents from the Docs service.
#This is the code that is causing error. -----------------------------
service.users().messages().batchDelete(userId='me').body({'ids':[
'[email protected]',
'[email protected]']
})
#-----------------------------
except HttpError as err:
print(err)
if __name__ == '__main__':
main()
This is the error/Traceback after running the program.
Traceback (most recent call last):
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line
31, in <module>
start(fakepyfile,mainpyfile)
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start
exec(open(mainpyfile).read(), __main__.__dict__)
File "<string>", line 54, in <module>
File "<string>", line 46, in main
TypeError: 'NoneType' object is not callable
[Program finished]
CodePudding user response:
Some things to notice on the code provided:
- The call to
service.users().messages().batchDeleteis not indented inside thetryblock. This might have broken when pasting it here, but it is worth mentioning since this is python code. bodyis not a method, therefore calling like.body(...)will return an exception. In fact,bodyis the name of the argument of thebatchDeletemethod. Here is the specific python client documentation for this method.- The field
idsof the requestbodyexpectsmessage idsrather than email addresses. This is an API specification (unrelated to the python client) documented here. There are several methods to retrieve a message id in the Gmail API. For example the method users.messages.list() - Creating the request structure and populating the payload does not mean it actually make the HTTP request. You must call the method
execute(). This scenario is documented here.
Fix suggestion:
...
service.users().messages().batchDelete(userId='me', body={'ids':[
'999999f99ff9e99e',
'9999a99c99c99d99']
}).execute()
...
