<s> import os <EOL> import rauth <EOL> import webbrowser <EOL> from . import utils <EOL> from . utils . api import GraphAPI <EOL> """<STR_LIT>""" <EOL> PORT = <NUM_LIT> <EOL> REDIRECT_URI = '<STR_LIT>' . format ( PORT ) <EOL> class OAuth2Service ( rauth . OAuth2Service ) : <EOL> def __init__ ( self , * vargs , ** kwargs ) : <EOL> options = dict ( <EOL> authorize_url = '<STR_LIT>' , <EOL> access_token_url = '<STR_LIT>' , <EOL> base_url = '<STR_LIT>' , <EOL> ) <EOL> options . update ( ** kwargs ) <EOL> self . redirect_uri = options . get ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' in options : <EOL> del options [ '<STR_LIT>' ] <EOL> super ( OAuth2Service , self ) . __init__ ( * vargs , ** options ) <EOL> def get_authorize_url ( self , * vargs , ** kwargs ) : <EOL> options = dict ( <EOL> scope = '<STR_LIT>' , <EOL> response_type = '<STR_LIT:code>' , <EOL> redirect_uri = self . redirect_uri , <EOL> ) <EOL> options . update ( ** kwargs ) <EOL> return super ( OAuth2Service , self ) . get_authorize_url ( * vargs , ** options ) <EOL> def get_access_token ( self , code , * vargs , ** kwargs ) : <EOL> long_term = kwargs . get ( '<STR_LIT>' , False ) <EOL> if '<STR_LIT>' in kwargs : <EOL> del kwargs [ '<STR_LIT>' ] <EOL> data = dict ( <EOL> code = code , <EOL> grant_type = '<STR_LIT>' , <EOL> redirect_uri = self . redirect_uri , <EOL> ) <EOL> data . update ( kwargs . get ( '<STR_LIT:data>' , { } ) ) <EOL> kwargs [ '<STR_LIT:data>' ] = data <EOL> token = super ( OAuth2Service , self ) . get_access_token ( <EOL> * vargs , ** kwargs ) <EOL> if long_term : <EOL> token = self . get_long_term_token ( token ) <EOL> return token <EOL> def get_long_term_token ( self , short_token ) : <EOL> graph = GraphAPI ( ) <EOL> data = graph . get ( '<STR_LIT>' , <EOL> grant_type = '<STR_LIT>' , <EOL> client_id = self . client_id , <EOL> client_secret = self . client_secret , <EOL> fb_exchange_token = short_token , <EOL> ) <EOL> token = dict ( utils . url . parse . parse_qsl ( data ) ) <EOL> return token [ '<STR_LIT>' ] <EOL> def get_page_tokens ( self , long_token ) : <EOL> graph = GraphAPI ( long_token ) <EOL> accounts = graph . get ( '<STR_LIT>' ) [ '<STR_LIT:data>' ] <EOL> return [ account [ '<STR_LIT>' ] for account in accounts ] <EOL> def authorize_user ( facebook , long_term = False ) : <EOL> authorize_url = facebook . get_authorize_url ( ) <EOL> webbrowser . open ( authorize_url ) <EOL> qs = utils . server . single_serve ( <EOL> message = '<STR_LIT>' , <EOL> port = PORT , <EOL> ) <EOL> return facebook . get_access_token ( qs [ '<STR_LIT:code>' ] , long_term = long_term ) <EOL> def authorize_pages ( facebook , user_token ) : <EOL> return facebook . get_page_tokens ( user_token ) <EOL> def authorize ( client_id , client_secret ) : <EOL> facebook = OAuth2Service ( <EOL> client_id = client_id , <EOL> client_secret = client_secret , <EOL> redirect_uri = REDIRECT_URI , <EOL> ) <EOL> user_token = authorize_user ( facebook , long_term = True ) <EOL> page_tokens = authorize_pages ( facebook , user_token ) <EOL> return page_tokens </s>
