|
|
|
from googleapiclient.discovery import build |
|
from google_auth_oauthlib.flow import InstalledAppFlow |
|
from google.auth.transport.requests import Request |
|
import httplib2 |
|
|
|
SCOPES = ['https://www.googleapis.com/auth/gmail.readonly'] |
|
credential_file = './client_secret.json' |
|
|
|
proxies = { |
|
'http': 'http://proxy.example.com:8080', |
|
'https': 'http://proxy.example.com:8080', |
|
} |
|
USER_AGENT='Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36' |
|
headers={'User-Agent': USER_AGENT, 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'} |
|
import socks |
|
http=httplib2.Http(proxy_info = httplib2.ProxyInfo(socks.PROXY_TYPE_HTTP, 'proxy.example.com', 8080)) |
|
#http.putheader("User-Agent", USER_AGENT) |
|
|
|
# oauth |
|
flow = InstalledAppFlow.from_client_secrets_file( |
|
credential_file, SCOPES, |
|
redirect_uri='urn:ietf:wg:oauth:2.0:oob') |
|
auth_url, _ = flow.authorization_url(prompt='consent') |
|
print('Please visit this URL to authorize this application: {url}'.format(url=auth_url)) |
|
code = input('Enter the authorization code: ') |
|
flow.fetch_token(code=code, headers=headers, proxies=proxies) |
|
creds = flow.credentials |
|
#session = flow.authorized_session() |
|
|
|
# gmail |
|
service = build('gmail', 'v1', credentials=creds, http=http) |
|
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']) |
|
|