Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.py
More file actions
Latest commit
52 lines (43 loc) · 1.62 KB
/
Copy pathbase.py
File metadata and controls
52 lines (43 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
importpickle
importos.path
fromgoogleapiclient.discoveryimportbuild
fromgoogle_auth_oauthlib.flowimportInstalledAppFlow
fromgoogle.auth.transport.requestsimportRequest
importutilsasut
# If modifying these scopes, delete the file token.pickle.
SCOPES= ['https://www.googleapis.com/auth/gmail.readonly']
defmain():
"""Shows basic usage of the Gmail API.
Lists the user's Gmail labels.
"""
creds=None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
ifos.path.exists('token.pickle'):
withopen('token.pickle', 'rb') astoken:
creds=pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
ifnotcredsornotcreds.valid:
ifcredsandcreds.expiredandcreds.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
withopen('token.pickle', 'wb') astoken:
pickle.dump(creds, token)
service=build('gmail', 'v1', credentials=creds)
# Call the Gmail API
results=service.users().labels().list(userId='me').execute()
labels=results.get('labels', [])
# if not labels:
# print('No labels found.')
# else:
# print('Labels:')
# for label in labels:
# print(label['name'])
returnservice
# if __name__ == '__main__':
# main()