Skip to content

Instantly share code, notes, and snippets.

@kyokuheki
Last active April 18, 2019 16:12
Show Gist options
  • Save kyokuheki/7fad48f45d132877b51ac27ef157cfae to your computer and use it in GitHub Desktop.
Save kyokuheki/7fad48f45d132877b51ac27ef157cfae to your computer and use it in GitHub Desktop.
  1. google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file
    • args: **kwargs
    • call: google_auth_oauthlib.flow.InstalledAppFlow.from_client_config
      • args: **kwargs
  2. google_auth_oauthlib.flow.InstalledAppFlow.from_client_config
    • call: google_auth_oauthlib.helpers.session_from_client_config
      • args: **kwargs
  3. google_auth_oauthlib.helpers.session_from_client_config
  4. requests_oauthlib.OAuth2Session.__init__
    • call: requests.Session.__init__
      • args: **kwargs
  5. Flow.__init__
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'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment