import json from flask import Flask, request from threading import Timer from IPython.display import IFrame from IPython.display import display from IPython.display import Javascript as JS import twitter from twitter.oauth_dance import parse_oauth_tokens from twitter.oauth import read_token_file, write_token_file OAUTH_FILE = "/tmp/twitter_oauth" # XXX: Go to http://twitter.com/apps/new to create an app and get values # for these credentials that you'll need to provide in place of these # empty string values that are defined as placeholders. # See https://dev.twitter.com/docs/auth/oauth for more information # on Twitter's OAuth implementation and ensure that *oauth_callback* # is defined in your application settings as shown below if you are # using Flask in this IPython Notebook # Define a few variables that will bleed into the lexical scope of a couple of # functions below CONSUMER_KEY = '' CONSUMER_SECRET = '' oauth_callback = 'http://127.0.0.1:5000/oauth_helper' # Setup a callback handler for when Twitter redirects back to us after the user authorizes the app webserver = Flask("TwitterOAuth") @webserver.route("/oauth_helper") def oauth_helper(): oauth_verifier = request.args.get('oauth_verifier') # Pick back up credentials from ipynb_oauth_dance oauth_token, oauth_token_secret = read_token_file(OAUTH_FILE) _twitter = twitter.Twitter( auth=twitter.OAuth( oauth_token, oauth_token_secret, CONSUMER_KEY, CONSUMER_SECRET), format='', api_version=None) oauth_token, oauth_token_secret = parse_oauth_tokens( _twitter.oauth.access_token(oauth_verifier=oauth_verifier)) # This web server only needs to service one request, so shut it down shutdown_after_request = request.environ.get('werkzeug.server.shutdown') shutdown_after_request() # Write out the final credentials that can be picked up after the blocking # call to webserver.run() below. write_token_file(OAUTH_FILE, oauth_token, oauth_token_secret) return "%s %s written to %s" % (oauth_token, oauth_token_secret, OAUTH_FILE) # To handle Twitter's OAuth 1.0a implementation, we'll just need to implement a custom # "oauth dance" and will closely follower the pattern defined in twitter.oauth_dance. def ipynb_oauth_dance(): _twitter = twitter.Twitter( auth=twitter.OAuth('', '', CONSUMER_KEY, CONSUMER_SECRET), format='', api_version=None) oauth_token, oauth_token_secret = parse_oauth_tokens( _twitter.oauth.request_token(oauth_callback=oauth_callback)) # Need to write these interim values out to a file to pick up on the callback from Twitter # that is handled by the web server in /oauth_helper write_token_file(OAUTH_FILE, oauth_token, oauth_token_secret) oauth_url = ('http://api.twitter.com/oauth/authorize?oauth_token=' + oauth_token) # Tap the browser's native capabilities to access the web server through a new window to get # user authorization display(JS("window.open('%s')" % oauth_url)) # After the webserver.run() blocking call, start the oauth dance that will ultimately # cause Twitter to redirect a request back to it. Once that request is serviced, the web # server will shutdown, and program flow will resume with the OAUTH_FILE containing the # necessary credentials Timer(1, lambda: ipynb_oauth_dance()).start() webserver.run(host='0.0.0.0') # The values that are read from this file are written out at # the end of /oauth_helper oauth_token, oauth_token_secret = read_token_file(OAUTH_FILE) # These 4 credentials are what is needed to authorize the application auth = twitter.oauth.OAuth(oauth_token, oauth_token_secret, CONSUMER_KEY, CONSUMER_SECRET) twitter_api = twitter.Twitter(auth=auth) print twitter_api import urllib from flask import Flask, request from threading import Timer from IPython.display import display from IPython.display import Javascript as JS # XXX: Get this value from your Facebook application's settings for the OAuth flow # at https://developers.facebook.com/apps APP_ID = '' # This value is where Facebook will redirect. We'll configure an embedded # web server to be serving requests here REDIRECT_URI = 'http://localhost:5000/oauth_helper' # You could customize which extended permissions are being requested for your app # by adding additional items to the list below. See # https://developers.facebook.com/docs/reference/login/extended-permissions/ EXTENDED_PERMS = ['user_likes', 'friends_likes'] # A temporary file to store a code from the web server OAUTH_FILE = 'resources/ch02-facebook/access_token.txt' # Configure an emedded web server that accepts one request, parses # the fragment identifier out of the browser window redirects to another # handler with the parsed out value in the query string where it can be captured # and stored to disk. (A webserver cannot capture information in the fragment # identifier or that work would simply be done in here.) webserver = Flask("FacebookOAuth") @webserver.route("/oauth_helper") def oauth_helper(): return '''''' # Parses out a query string parameter and stores it to disk. This is required because # the address space that Flask uses is not shared with IPython Notebook, so there is really # no other way to share the information than to store it to a file and access it afterward @webserver.route("/access_token_capture") def access_token_capture(): access_token = request.args.get('access_token') f = open(OAUTH_FILE, 'w') # Store the code as a file f.write(access_token) f.close() # It is safe (and convenient) to shut down the web server after this request shutdown_after_request = request.environ.get('werkzeug.server.shutdown') shutdown_after_request() return access_token # Send an OAuth request to Facebook, handle the redirect, and display the access # token that's included in the redirect for the user to copy and paste args = dict(client_id=APP_ID, redirect_uri=REDIRECT_URI, scope=','.join(EXTENDED_PERMS), type='user_agent', display='popup' ) oauth_url = 'https://facebook.com/dialog/oauth?' + urllib.urlencode(args) Timer(1, lambda: display(JS("window.open('%s')" % oauth_url))).start() webserver.run(host='0.0.0.0') access_token = open(OAUTH_FILE).read() print access_token import os from threading import Timer from flask import Flask, request from linkedin import linkedin from IPython.display import display from IPython.display import Javascript as JS # XXX: Get these values from your application's settings for the OAuth flow CONSUMER_KEY = '' CONSUMER_SECRET = '' # This value is where LinkedIn will redirect. We'll configure an embedded # web server to be serving requests here RETURN_URL = 'http://localhost:5000/oauth_helper' # A temporary file to store a code from the web server OAUTH_FILE = 'resources/ch03-linkedin/linkedin.authorization_code' # Configure an emedded web server that accepts one request, stores a file # that will need to be accessed outside of the request context, and # immediately shuts itself down webserver = Flask("OAuthHelper") @webserver.route("/oauth_helper") def oauth_helper(): code = request.args.get('code') f = open(OAUTH_FILE, 'w') # Store the code as a file f.write(code) f.close() shutdown_after_request = request.environ.get('werkzeug.server.shutdown') shutdown_after_request() return """

Handled redirect and extracted code %s for authorization

""" % (code,) # Send an OAuth request to LinkedIn, handle the redirect, and display the access # token that's included in the redirect for the user to copy and paste auth = linkedin.LinkedInAuthentication(CONSUMER_KEY, CONSUMER_SECRET, RETURN_URL, linkedin.PERMISSIONS.enums.values()) # Display popup after a brief delay to ensure that the web server is running and # can handle the redirect back from LinkedIn Timer(1, lambda: display(JS("window.open('%s')" % auth.authorization_url))).start() # Run the server to accept the redirect back from LinkedIn and capture the access # token. This command blocks, but the web server is configured to shut itself down # after it serves a request, so after the redirect occurs, program flow will continue webserver.run(host='0.0.0.0') # As soon as the request finishes, the web server shuts down and these remaining commands # are executed, which exchange an authorization code for an access token. This process # seems to need full automation because the authorization code expires very quickly. auth.authorization_code = open(OAUTH_FILE).read() auth.get_access_token() # Prevent stale tokens from sticking around, which could complicate debugging os.remove(OAUTH_FILE) # How you can use the application to access the LinkedIn API... app = linkedin.LinkedInApplication(auth) print app.get_profile()