Skip to content

Instantly share code, notes, and snippets.

@rjmcguire
Forked from cdellin/getoauthcode.py
Last active September 15, 2016 06:04
Show Gist options
  • Save rjmcguire/8b49dcebc813fff65a7512de36302dd1 to your computer and use it in GitHub Desktop.
Save rjmcguire/8b49dcebc813fff65a7512de36302dd1 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2
# Author: Chris Dellin <[email protected]>
# Date: 2016-09-14
# Script to scrape a working OAuth code from Google
# using the approach from dequis [see: https://github.com/tdryer/hangups/issues/260#issuecomment-247234371]
# for use with hangups, purple-hangouts, etc.
import pygtk
pygtk.require('2.0')
from gi.repository import Gtk as gtk
from gi.repository import WebKit as webkit
from gi.repository import GObject as gobject
from gi.repository import Soup as soup
# create window with a webview
window = gtk.Window()
window.set_resizable(True)
window.set_default_size(350,580)
window.connect("delete_event", lambda w,e: False)
window.connect("destroy", lambda w: gtk.main_quit())
scrolled = gtk.ScrolledWindow(None, None)
webview = webkit.WebView()
scrolled.add(webview)
window.add(scrolled)
window.show_all()
# hook up a cookie jar
# http://stackoverflow.com/a/39296977/5228520
cookiejar = soup.CookieJarText.new('', False)
cookiejar.set_accept_policy(soup.CookieJarAcceptPolicy.ALWAYS)
session = webkit.get_default_session()
session.add_feature(cookiejar)
# open google page to request auth code
# see https://github.com/tdryer/hangups/issues/260#issuecomment-246578670
# thanks dequis!
uri_entrypoint = 'https://accounts.google.com/o/oauth2/programmatic_auth?hl=en&scope=https%3A%2F%2Fwww.google.com%2Faccounts%2FOAuthLogin+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email&client_id=936475272427.apps.googleusercontent.com&access_type=offline&delegated_client_id=183697946088-m3jnlsqshjhh5lbvg05k46q1k4qqtrgn.apps.googleusercontent.com&top_level_cookie=1'
webview.open(uri_entrypoint)
# register callback to auto-close on success or failure
def mycallback():
uri = webview.get_uri()
if uri is None or uri.startswith('https://accounts.google.com/ServiceLogin'):
return True
gtk.main_quit()
gobject.timeout_add(100, mycallback)
# start application (stops on window close or callback)
gobject.threads_init()
gtk.main()
# read from cookie jar
oauth_codes = []
for c in cookiejar.all_cookies():
if c.get_domain() == 'accounts.google.com' and c.get_name() == 'oauth_code':
oauth_codes.append(c.get_value())
for oauth_code in oauth_codes:
print('Found oauth_code: {}'.format(oauth_code))
if not oauth_codes:
print('No oauth_codes found!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment