#NEWLINE# def try_micropub_config( token_url , micropub_url , scopes , code , me , redirect_uri , client_id , state ) : #NEWLINE# #INDENT# if ( ( not scopes ) or ( 'config' not in scopes ) or ( not token_url ) or ( not micropub_url ) ) : #NEWLINE# #INDENT# flash ( 'Micropub not supported (which is fine).' ) #NEWLINE# return False #NEWLINE# #UNINDENT# token_response = requests . post ( token_url , data = { #NEWLINE# #INDENT# 'code' : code , #NEWLINE# 'me' : me , #NEWLINE# 'redirect_uri' : redirect_uri , #NEWLINE# 'client_id' : client_id , #NEWLINE# 'state' : code , #NEWLINE# #UNINDENT# } ) #NEWLINE# if ( token_response . status_code != 200 ) : #NEWLINE# #INDENT# flash ( 'Unexpected response from token endpoint {}' . format ( token_response ) ) #NEWLINE# return False #NEWLINE# #UNINDENT# tdata = urllib . parse . parse_qs ( token_response . text ) #NEWLINE# if ( 'access_token' not in tdata ) : #NEWLINE# #INDENT# flash ( 'Response from token endpoint missing access_token {}' . format ( tdata ) ) #NEWLINE# return False #NEWLINE# #UNINDENT# access_token = tdata . get ( 'access_token' ) [ 0 ] #NEWLINE# session [ 'micropub' ] = ( micropub_url , access_token ) #NEWLINE# flash ( 'Got micropub access token {}' . format ( ( access_token [ : 6 ] + '...' ) ) ) #NEWLINE# actions_response = requests . get ( ( micropub_url + '?q=actions' ) , headers = { #NEWLINE# #INDENT# 'Authorization' : ( 'Bearer ' + access_token ) , #NEWLINE# #UNINDENT# } ) #NEWLINE# if ( actions_response . status_code != 200 ) : #NEWLINE# #INDENT# current_app . logger . debug ( 'Bad response to action handler query %s' , actions_response ) #NEWLINE# return False #NEWLINE# #UNINDENT# current_app . logger . debug ( 'Successful action handler query %s' , actions_response . text ) #NEWLINE# actions_content_type = actions_response . headers . get ( 'content-type' , '' ) #NEWLINE# if ( 'application/json' in actions_content_type ) : #NEWLINE# #INDENT# adata = json . loads ( actions_response . text ) #NEWLINE# current_app . logger . debug ( 'action handlers (json): %s' , adata ) #NEWLINE# session [ 'action-handlers' ] = adata #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# adata = urllib . parse . parse_qs ( actions_response . text ) #NEWLINE# current_app . logger . debug ( 'action handlers: %s' , adata ) #NEWLINE# session [ 'action-handlers' ] = { key : value [ 0 ] for ( key , value ) in adata . items ( ) } #NEWLINE# #UNINDENT# return True