def buildFullUrl(path, parametersArray) : full_url = path if parametersArray is not None: url_values = urllib.urlencode(parametersArray) full_url = full_url + '?' + url_values customFunctions.printToLog('buildFullUrl: ' + full_url, 1) return full_url
def index(): customFunctions.printToLog('------------------------------------------------', 0) customFunctions.printToLog('landingPageSpotify()', 1) #Get the user's email address authorizedUserEmailAddress = helperGetUserEmailAddress() #Get the user's saved tracks savedTracks = helperGetSavedTracks() return dict(message='Authenticated with Spotify as: ' + authorizedUserEmailAddress, savedTracks = savedTracks)
def postToTokenEndpointHelper(postUrl, grantType, codeParameterForPostRequest, oAuthRedirectUri, client_id, client_secret) : requestBodyParameters = {'grant_type' : grantType, 'code' : codeParameterForPostRequest, 'redirect_uri' : oAuthRedirectUri, 'client_id' : client_id, 'client_secret' : client_secret} #Call the function to send the HTTP POST and get the response responseFromPost = httpFunctions.postRequest(postUrl, requestBodyParameters) #Parse the response and return the data to the caller. responseDataInJson = responseFromPost.read() responseDataInArray = httpFunctions.convertJsonToArray(responseDataInJson) customFunctions.printToLog('postToTokenEndpointSpotify: ' + str(responseDataInArray), 1) return responseDataInArray
def addOauthSessionVariable(session, oAuthVariableType, value, resourceOwner=None): customFunctions.printToLog( 'addOauthSessionVariable: oAuthVariableType: ' + str(oAuthVariableType), 0) customFunctions.printToLog( 'addOauthSessionVariable: resourceOwner: ' + str(resourceOwner), 0) customFunctions.printToLog('addOauthSessionVariable: value: ' + str(value), 1) if oAuthVariableType == 'access_token': session.access_token = session.access_token or {} session.access_token[resourceOwner] = value elif oAuthVariableType == 'token_type': session.token_type = session.token_type or {} session.token_type[resourceOwner] = value elif oAuthVariableType == 'expires_in': session.expires_in = session.expires_in or {} session.expires_in[resourceOwner] = value elif oAuthVariableType == 'refresh_token': session.refresh_token = session.refresh_token or {} session.refresh_token[resourceOwner] = value else: customFunctions.printToLog('addOauthSessionVariable: error', 1)
def POST(*args,**vars): #Required parameter: 'resourceOwnerUrl' #Contains the url of the endpoint that this request should be forwarded to. resourceOwnerUrl = request.post_vars['resourceOwnerUrl'] customFunctions.printToLog('generateAuthenticatedRequestToUrl POST: resourceOwnerUrl: ' + resourceOwnerUrl, 0) #Optional parameter: 'accesss_token' #Contains the oAuth Access Token that should be put in the header of the request that we forward to the resource owner. Not all requests #require an Access Token. access_token = request.post_vars['access_token'] customFunctions.printToLog('generateAuthenticatedRequestToUrl POST: access_token: ' + access_token, 0) #There may be an HTTP POST parameter called 'jsonString' which is a JSON string of arbitrary key-value pairs. These will be loaded #into the request that we forward to the resource owner. jsonString = request.post_vars['jsonString'] parametersArray = None if jsonString is not None: parametersArray = httpFunctions.convertJsonToArray(jsonString) for key, value in parametersArray.iteritems(): customFunctions.printToLog('generateAuthenticatedRequestToUrl POST: key: ' + key, 0) customFunctions.printToLog('generateAuthenticatedRequestToUrl POST: value: ' + value, 0) #Build the headers for this request headers = {} #If an access token was supplied, add it to the token if access_token is not None: authorizationHeader = 'Bearer ' + access_token headers['Authorization'] = authorizationHeader #Send the request and receive the response response = httpFunctions.getRequest(resourceOwnerUrl, None, headers) return response
def index(): customFunctions.printToLog('------------------------------------------------', 0) customFunctions.printToLog('index()', 0) ############################## #If we have a parameter 'code', that means we've been redirected to this page from the "authorize" endpoint. parameterCode = request.vars['code'] parameterError = request.vars['error'] if parameterError is not None: customFunctions.printToLog('URL parameter \'error\': ' + parameterError, 1) elif parameterCode is not None: #Generate an HTTP POST to the "token" endpoint and save the results to the session. customFunctions.printToLog('URL parameter \'code\': ' + parameterCode, 1) responseDataInArray = contextSensitiveFunctions.callPostToTokenEndpointAuthorizationCode('Spotify', parameterCode) #Store data to session contextSensitiveFunctions.addOauthSessionVariable(session, 'access_token', responseDataInArray['access_token'], 'Spotify') contextSensitiveFunctions.addOauthSessionVariable(session, 'token_type', responseDataInArray['token_type'], 'Spotify') contextSensitiveFunctions.addOauthSessionVariable(session, 'expires_in', responseDataInArray['expires_in'], 'Spotify') contextSensitiveFunctions.addOauthSessionVariable(session, 'refresh_token', responseDataInArray['refresh_token'], 'Spotify') #Now that the Access Token has been saved to session, redirect the the landing page for this resource. redirect(URL('spotify', 'index')) ############################## #Build "authorize" URL that, when the user is redirected there, will begin the OAuth handshake full_url_spotify = contextSensitiveFunctions.callBuildUrlToInitiateAuthorization('Spotify') ############################## #response.flash = T("Welcome to the Spotify app!") return dict(message=T('Hello World'), authenticate_url_spotify=full_url_spotify)
def index(): customFunctions.printToLog( '------------------------------------------------', 0) customFunctions.printToLog('landingPageSpotify()', 1) #Get the user's email address authorizedUserEmailAddress = helperGetUserEmailAddress() #Get the user's saved tracks savedTracks = helperGetSavedTracks() return dict(message='Authenticated with Spotify as: ' + authorizedUserEmailAddress, savedTracks=savedTracks)
def postToTokenEndpointHelper(postUrl, grantType, codeParameterForPostRequest, oAuthRedirectUri, client_id, client_secret): requestBodyParameters = { 'grant_type': grantType, 'code': codeParameterForPostRequest, 'redirect_uri': oAuthRedirectUri, 'client_id': client_id, 'client_secret': client_secret } #Call the function to send the HTTP POST and get the response responseFromPost = httpFunctions.postRequest(postUrl, requestBodyParameters) #Parse the response and return the data to the caller. responseDataInJson = responseFromPost.read() responseDataInArray = httpFunctions.convertJsonToArray(responseDataInJson) customFunctions.printToLog( 'postToTokenEndpointSpotify: ' + str(responseDataInArray), 1) return responseDataInArray
def GET(resourceOwner, configSetting): #Log inputs customFunctions.printToLog('getConfigValue GET: resourceOwner: ' + resourceOwner, 0) customFunctions.printToLog('getConfigValue GET: configSetting: ' + configSetting, 0) #Get the config value configVal = apiFunctions.getConfigValueHelper(db, resourceOwner, configSetting) #Log the result and return it to the caller customFunctions.printToLog('getConfigValue GET: configVal: ' + configVal, 1) return configVal
def addOauthSessionVariable(session, oAuthVariableType, value, resourceOwner = None) : customFunctions.printToLog('addOauthSessionVariable: oAuthVariableType: ' + str(oAuthVariableType), 0) customFunctions.printToLog('addOauthSessionVariable: resourceOwner: ' + str(resourceOwner), 0) customFunctions.printToLog('addOauthSessionVariable: value: ' + str(value), 1) if oAuthVariableType == 'access_token': session.access_token = session.access_token or {} session.access_token[resourceOwner] = value elif oAuthVariableType == 'token_type': session.token_type = session.token_type or {} session.token_type[resourceOwner] = value elif oAuthVariableType == 'expires_in': session.expires_in = session.expires_in or {} session.expires_in[resourceOwner] = value elif oAuthVariableType == 'refresh_token': session.refresh_token = session.refresh_token or {} session.refresh_token[resourceOwner] = value else: customFunctions.printToLog('addOauthSessionVariable: error', 1)
def GET(resourceOwner, configSetting): #Log inputs customFunctions.printToLog( 'getConfigValue GET: resourceOwner: ' + resourceOwner, 0) customFunctions.printToLog( 'getConfigValue GET: configSetting: ' + configSetting, 0) #Get the config value configVal = apiFunctions.getConfigValueHelper(db, resourceOwner, configSetting) #Log the result and return it to the caller customFunctions.printToLog( 'getConfigValue GET: configVal: ' + configVal, 1) return configVal
def postRequest(url, parametersArray = None, headersArray = None) : #Logging if parametersArray is not None: for key, value in parametersArray.iteritems(): customFunctions.printToLog('postRequest: parametersArray: ' + key + ': ' + value, 0) if headersArray is not None: for key, value in headersArray.iteritems(): customFunctions.printToLog('postRequest: headersArray: ' + key + ': ' + value, 0) customFunctions.printToLog('postRequest: url: ' + url, 1) #Send the request and return the response to caller. data = urllib.urlencode(parametersArray) req = urllib2.Request(url, data) response = urllib2.urlopen(req) return response
def POST(*args, **vars): #Required parameter: 'resourceOwnerUrl' #Contains the url of the endpoint that this request should be forwarded to. resourceOwnerUrl = request.post_vars['resourceOwnerUrl'] customFunctions.printToLog( 'generateAuthenticatedRequestToUrl POST: resourceOwnerUrl: ' + resourceOwnerUrl, 0) #Optional parameter: 'accesss_token' #Contains the oAuth Access Token that should be put in the header of the request that we forward to the resource owner. Not all requests #require an Access Token. access_token = request.post_vars['access_token'] customFunctions.printToLog( 'generateAuthenticatedRequestToUrl POST: access_token: ' + access_token, 0) #There may be an HTTP POST parameter called 'jsonString' which is a JSON string of arbitrary key-value pairs. These will be loaded #into the request that we forward to the resource owner. jsonString = request.post_vars['jsonString'] parametersArray = None if jsonString is not None: parametersArray = httpFunctions.convertJsonToArray(jsonString) for key, value in parametersArray.iteritems(): customFunctions.printToLog( 'generateAuthenticatedRequestToUrl POST: key: ' + key, 0) customFunctions.printToLog( 'generateAuthenticatedRequestToUrl POST: value: ' + value, 0) #Build the headers for this request headers = {} #If an access token was supplied, add it to the token if access_token is not None: authorizationHeader = 'Bearer ' + access_token headers['Authorization'] = authorizationHeader #Send the request and receive the response response = httpFunctions.getRequest(resourceOwnerUrl, None, headers) return response
def getRequest(url, parametersArray = None, headersArray = None) : #Logging if parametersArray is not None: for key, value in parametersArray.iteritems(): customFunctions.printToLog('getRequest: parametersArray: ' + key + ': ' + value, 0) if headersArray is not None: for key, value in headersArray.iteritems(): customFunctions.printToLog('getRequest: headersArray: ' + key + ': ' + value, 0) customFunctions.printToLog('getRequest: url: ' + url, 1) #Send the request and return the response to caller. #Build the final URL and the Request object full_url = buildFullUrl(url, parametersArray) req = urllib2.Request(full_url) #Loop through array of headers and add them to the request headers. if headersArray is not None: for key, value in headersArray.iteritems(): req.add_header(key,value) #Send the request and get the response response = urllib2.urlopen(req) responseData = response.read() customFunctions.printToLog('getRequest: ' + responseData, 1) return responseData
def getOauthSessionVariable(session, oAuthVariableType, resourceOwner = None) : customFunctions.printToLog('getOauthSessionVariable: oAuthVariableType: ' + str(oAuthVariableType), 0) customFunctions.printToLog('getOauthSessionVariable: resourceOwner: ' + str(resourceOwner), 0) if oAuthVariableType == 'access_token': session.access_token = session.access_token or {} returnValue = session.access_token[resourceOwner] elif oAuthVariableType == 'token_type': session.token_type = session.token_type or {} returnValue = session.token_type[resourceOwner] elif oAuthVariableType == 'expires_in': session.expires_in = session.expires_in or {} returnValue = session.expires_in[resourceOwner] elif oAuthVariableType == 'refresh_token': session.refresh_token = session.refresh_token or {} returnValue = session.refresh_token[resourceOwner] else: returnValue = None customFunctions.printToLog('getOauthSessionVariable: returnValue: ' + str(returnValue), 1) return returnValue
def getOauthSessionVariable(session, oAuthVariableType, resourceOwner=None): customFunctions.printToLog( 'getOauthSessionVariable: oAuthVariableType: ' + str(oAuthVariableType), 0) customFunctions.printToLog( 'getOauthSessionVariable: resourceOwner: ' + str(resourceOwner), 0) if oAuthVariableType == 'access_token': session.access_token = session.access_token or {} returnValue = session.access_token[resourceOwner] elif oAuthVariableType == 'token_type': session.token_type = session.token_type or {} returnValue = session.token_type[resourceOwner] elif oAuthVariableType == 'expires_in': session.expires_in = session.expires_in or {} returnValue = session.expires_in[resourceOwner] elif oAuthVariableType == 'refresh_token': session.refresh_token = session.refresh_token or {} returnValue = session.refresh_token[resourceOwner] else: returnValue = None customFunctions.printToLog( 'getOauthSessionVariable: returnValue: ' + str(returnValue), 1) return returnValue
def GET(resourceOwner, oAuthRedirectUri): #Fetch this Resource Owner's configuration values authorization_endpoint = apiFunctions.getConfigValueHelper(db, resourceOwner, 'authorization_endpoint') client_id = apiFunctions.getConfigValueHelper(db, resourceOwner, 'client_id') response_type = apiFunctions.getConfigValueHelper(db, resourceOwner, 'response_type') scopes = apiFunctions.getConfigValueHelper(db, resourceOwner, 'scopes') show_dialog = apiFunctions.getConfigValueHelper(db, resourceOwner, 'show_dialog') #Log inputs customFunctions.printToLog('buildUrlToInitiateAuthorization GET: authorization_endpoint: ' + authorization_endpoint, 0) customFunctions.printToLog('buildUrlToInitiateAuthorization GET: client_id: ' + client_id, 0) customFunctions.printToLog('buildUrlToInitiateAuthorization GET: response_type: ' + response_type, 0) customFunctions.printToLog('buildUrlToInitiateAuthorization GET: scopes: ' + scopes, 0) customFunctions.printToLog('buildUrlToInitiateAuthorization GET: show_dialog: ' + show_dialog, 0) customFunctions.printToLog('buildUrlToInitiateAuthorization GET: oAuthRedirectUri: ' + oAuthRedirectUri, 0) #Build the url data = {} data['client_id'] = client_id data['response_type'] = response_type data['scope'] = scopes data['show_dialog'] = show_dialog data['redirect_uri'] = oAuthRedirectUri url = httpFunctions.buildFullUrl(authorization_endpoint, data) customFunctions.printToLog('buildUrlToInitiateAuthorization GET: url: ' + oAuthRedirectUri, 1) #Return the url to caller return url
def convertArrayToJson(array) : jsonObject = json.dumps(array) customFunctions.printToLog('convertArrayToJson: ' + jsonObject, 1) return jsonObject
def GET(resourceOwner, codeParameterForPostRequest, oAuthRedirectUri): #Fetch this Resource Owner's configuration values postUrl = apiFunctions.getConfigValueHelper(db, resourceOwner, 'token_endpoint') client_id = apiFunctions.getConfigValueHelper(db, resourceOwner, 'client_id') client_secret = apiFunctions.getConfigValueHelper(db, resourceOwner, 'client_secret') #Log inputs customFunctions.printToLog('postToTokenEndpointAuthorizationCode GET: postUrl: ' + postUrl, 0) customFunctions.printToLog('postToTokenEndpointAuthorizationCode GET: client_id: ' + client_id, 0) customFunctions.printToLog('postToTokenEndpointAuthorizationCode GET: client_secret: ' + client_secret, 0) customFunctions.printToLog('postToTokenEndpointAuthorizationCode GET: codeParameterForPostRequest: ' + codeParameterForPostRequest, 0) customFunctions.printToLog('postToTokenEndpointAuthorizationCode GET: oAuthRedirectUri: ' + oAuthRedirectUri, 0) #Call the function to generate the HTTP POST request and receive an array containing the response data from the Resource Owner. responseDataInArray = postToTokenEndpointHelper(postUrl, 'authorization_code', codeParameterForPostRequest, oAuthRedirectUri, client_id, client_secret) #Convert the array to a JSON object, log it, and return it to the caller. jsonObject = httpFunctions.convertArrayToJson(responseDataInArray) customFunctions.printToLog('postToTokenEndpointAuthorizationCode GET: jsonObject: ' + jsonObject, 1) return jsonObject
def convertJsonToArray(jsonObject) : customFunctions.printToLog('convertJsonToArray: ' + jsonObject, 1) pythonArray = json.loads(jsonObject) return pythonArray
def GET(resourceOwner, oAuthRedirectUri): #Fetch this Resource Owner's configuration values authorization_endpoint = apiFunctions.getConfigValueHelper( db, resourceOwner, 'authorization_endpoint') client_id = apiFunctions.getConfigValueHelper(db, resourceOwner, 'client_id') response_type = apiFunctions.getConfigValueHelper( db, resourceOwner, 'response_type') scopes = apiFunctions.getConfigValueHelper(db, resourceOwner, 'scopes') show_dialog = apiFunctions.getConfigValueHelper( db, resourceOwner, 'show_dialog') #Log inputs customFunctions.printToLog( 'buildUrlToInitiateAuthorization GET: authorization_endpoint: ' + authorization_endpoint, 0) customFunctions.printToLog( 'buildUrlToInitiateAuthorization GET: client_id: ' + client_id, 0) customFunctions.printToLog( 'buildUrlToInitiateAuthorization GET: response_type: ' + response_type, 0) customFunctions.printToLog( 'buildUrlToInitiateAuthorization GET: scopes: ' + scopes, 0) customFunctions.printToLog( 'buildUrlToInitiateAuthorization GET: show_dialog: ' + show_dialog, 0) customFunctions.printToLog( 'buildUrlToInitiateAuthorization GET: oAuthRedirectUri: ' + oAuthRedirectUri, 0) #Build the url data = {} data['client_id'] = client_id data['response_type'] = response_type data['scope'] = scopes data['show_dialog'] = show_dialog data['redirect_uri'] = oAuthRedirectUri url = httpFunctions.buildFullUrl(authorization_endpoint, data) customFunctions.printToLog( 'buildUrlToInitiateAuthorization GET: url: ' + oAuthRedirectUri, 1) #Return the url to caller return url
def GET(resourceOwner, codeParameterForPostRequest, oAuthRedirectUri): #Fetch this Resource Owner's configuration values postUrl = apiFunctions.getConfigValueHelper(db, resourceOwner, 'token_endpoint') client_id = apiFunctions.getConfigValueHelper(db, resourceOwner, 'client_id') client_secret = apiFunctions.getConfigValueHelper( db, resourceOwner, 'client_secret') #Log inputs customFunctions.printToLog( 'postToTokenEndpointAuthorizationCode GET: postUrl: ' + postUrl, 0) customFunctions.printToLog( 'postToTokenEndpointAuthorizationCode GET: client_id: ' + client_id, 0) customFunctions.printToLog( 'postToTokenEndpointAuthorizationCode GET: client_secret: ' + client_secret, 0) customFunctions.printToLog( 'postToTokenEndpointAuthorizationCode GET: codeParameterForPostRequest: ' + codeParameterForPostRequest, 0) customFunctions.printToLog( 'postToTokenEndpointAuthorizationCode GET: oAuthRedirectUri: ' + oAuthRedirectUri, 0) #Call the function to generate the HTTP POST request and receive an array containing the response data from the Resource Owner. responseDataInArray = postToTokenEndpointHelper( postUrl, 'authorization_code', codeParameterForPostRequest, oAuthRedirectUri, client_id, client_secret) #Convert the array to a JSON object, log it, and return it to the caller. jsonObject = httpFunctions.convertArrayToJson(responseDataInArray) customFunctions.printToLog( 'postToTokenEndpointAuthorizationCode GET: jsonObject: ' + jsonObject, 1) return jsonObject