Skip to content

Instantly share code, notes, and snippets.

@andrewsomething
Created August 5, 2014 03:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewsomething/d5bf87e7035414384024 to your computer and use it in GitHub Desktop.
Save andrewsomething/d5bf87e7035414384024 to your computer and use it in GitHub Desktop.
Get an an OAuth2 token using the "implicit" flow in Python and Gtk+ w/ DigitalOcean as the example
from gi.repository import Gtk
from gi.repository import WebKit
import urlparse
BASE_URL = 'https://cloud.digitalocean.com/v1/oauth/'
CLIENT_ID = 'specialsnowflake'
CALLBACK_URL = 'http://andrewsomething.com'
class AuthWin(Gtk.Window):
def __init__(self):
super(AuthWin, self).__init__()
oauth_url = "{0}/authorize?response_type=token&client_id={1}&redirect_uri={2}&scope=read%20write".format(
BASE_URL, CLIENT_ID, CALLBACK_URL)
self.web = WebKit.WebView()
self.scrolled = Gtk.ScrolledWindow()
self.scrolled.add(self.web)
self.add(self.scrolled)
self.set_size_request(900, 640)
self.set_position(Gtk.WindowPosition.CENTER)
self.set_title("Authorize")
self.set_skip_taskbar_hint(True)
self.set_resizable(False)
self.set_default_size(900, 640)
self.web.load_uri(oauth_url)
self.show_all()
self.web.connect('navigation-policy-decision-requested',
self.navigation_callback)
def navigation_callback(self, view, frame, request, action, decision):
url = request.get_uri()
if "#access_token" in url:
self.hide()
res = urlparse.parse_qs(url)
token_type = res['token_type'][0]
expires_in = res['expires_in'][0]
access_token = res[CALLBACK_URL + '/#access_token'][0]
print access_token
return token_type, expires_in, access_token
win = AuthWin()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment