FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythoncode-tutorials/general/gmail-api/common.py at master · Josh-Work/pythoncode-tutorials · GitHub
Josh-Work
/
pythoncode-tutorials
Public
forked from
x4nth055/pythoncode-tutorials
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pythoncode-tutorials
/
general
/
gmail-api
/
common.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
43 lines (38 loc) · 1.69 KB
Breadcrumbs
pythoncode-tutorials
/
general
/
gmail-api
/
common.py
Copy path
File metadata and controls
43 lines (38 loc) · 1.69 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
os
import
pickle
# Gmail API utils
from
googleapiclient
.
discovery
import
build
from
google_auth_oauthlib
.
flow
import
InstalledAppFlow
from
google
.
auth
.
transport
.
requests
import
Request
# Request all access (permission to read/send/receive emails, manage the inbox, and more)
SCOPES
=
[
'https://mail.google.com/'
]
our_email
=
'our_email@gmail.com'
def
gmail_authenticate
():
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
if
os
.
path
.
exists
(
"token.pickle"
):
with
open
(
"token.pickle"
,
"rb"
)
as
token
:
creds
=
pickle
.
load
(
token
)
# if there are no (valid) credentials availablle, 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.pickle"
,
"wb"
)
as
token
:
pickle
.
dump
(
creds
,
token
)
return
build
(
'gmail'
,
'v1'
,
credentials
=
creds
)
def
search_messages
(
service
,
query
):
result
=
service
.
users
().
messages
().
list
(
userId
=
'me'
,
q
=
query
).
execute
()
messages
=
[ ]
if
'messages'
in
result
:
messages
.
extend
(
result
[
'messages'
])
while
'nextPageToken'
in
result
:
page_token
=
result
[
'nextPageToken'
]
result
=
service
.
users
().
messages
().
list
(
userId
=
'me'
,
q
=
query
,
pageToken
=
page_token
).
execute
()
if
'messages'
in
result
:
messages
.
extend
(
result
[
'messages'
])
return
messages
Back
|
FazBrowse Home
|
New Git URL