Skip to content

Instantly share code, notes, and snippets.

@PresGas
Forked from cdellin/getoauthcode.py
Last active September 26, 2016 14:54
Show Gist options
  • Save PresGas/8ed3a539d810f95dac226e87311c98df to your computer and use it in GitHub Desktop.
Save PresGas/8ed3a539d810f95dac226e87311c98df to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2
# Author: Chris Dellin <cdellin@gmail.com>
# Date: 2016-09-14
# Updated: Robert Freeman-Day <PresGas@gmail.com>
# Removed extra url bits as per https://github.com/tdryer/hangups/issues/260#issuecomment-247512300
# Script to scrape a working OAuth code from Google
# using the approach from dequis
# 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&client_id=936475272427.apps.googleusercontent.com'
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