def set_user_code(code, state): user = get_user_by_field("spotify.state", state) if (user != None): log.warning("set_user_code") uri = "https://accounts.spotify.com/api/token" payload = { "grant_type": "authorization_code", "code": code, "redirect_uri": redirect_uri } headers = { "Authorization": auth, 'Content-Type': 'application/x-www-form-urlencoded' } log.warning(redirect_uri) response = requests.post(uri, data=payload, headers=headers) log.warning(response) data = json.loads(response.text) log.warning(data) spotify_dict = { "user": user["spotify"]["user"], "state": state, "access_token": data["access_token"], "refresh_token": data["refresh_token"] } add_user_field(user["user"], "spotify", spotify_dict) devices = get_user_devices(user["user"]) add_user_field(user["user"], "spotify.devices", devices) return "Spotify authentication successful, click <a href=http://" + redirect_uri + "/connect/spotify/devices/" + user[ "spotify"][ "user"] + ">here</a> to get a list of your available devices." return 'Authorization request not set'
def get_connect_uri(username, service_username): scope = 'user-read-playback-state+user-modify-playback-state' state = ''.join( random.choice(string.ascii_uppercase + string.digits) for _ in range(10)) authorization_url = "https://accounts.spotify.com/authorize?client_id=" + client_id + "&response_type=code&redirect_uri=" + redirect_uri + "&scope=" + scope + "&state=" + state + "" spotify_dict = {"user": service_username, "state": state} add_user_field(username, "spotify", spotify_dict) log.warning(authorization_url) return '<a href="' + authorization_url + '">Connect with Spotify</a>'
def get_request_token(username,twitter_username): auth = tweepy.OAuthHandler(consumer_token, consumer_secret) try: redirect_url = auth.get_authorization_url() log.warning(redirect_url) twitter_dict={"oauth_token": auth.request_token["oauth_token"],"user":twitter_username } add_user_field(username, "twitter", twitter_dict) return '<a href="'+redirect_url+'">Connect with Twitter</a>' except tweepy.TweepError: log.warning('Error! Failed to get request token.') return 'Error! Failed to get request token.'
def refresh_access_token(user): uri = "https://accounts.spotify.com/api/token" payload = { "grant_type": "refresh_token", "refresh_token": user["spotify"]["refresh_token"] } headers = { "Authorization": auth, 'Content-Type': 'application/x-www-form-urlencoded' } response = requests.post(uri, data=payload, headers=headers) log.warning(response.text) data = json.loads(response.text) add_user_field(user["user"], "spotify.access_token", data["access_token"])
def set_user_credentials(uri, state): user = get_user_by_field("gmail.state", state) if (user != None): flow.fetch_token(authorization_response=uri) credentials = flow.credentials gmail_dict = { "state": state, "user": user["gmail"]["user"], "token": credentials.token, "refresh_token": credentials.refresh_token, "token_uri": credentials.token_uri, "scopes": credentials.scopes } add_user_field(user["user"], "gmail", gmail_dict) return "Gmail authentication successful" return 'CSRF Warning! State not equal in request and response.'
def set_user_token(oauth_token, oauth_verifier): user=get_user_by_field("twitter.oauth_token",oauth_token) if (user!=None): auth = tweepy.OAuthHandler(consumer_token, consumer_secret) auth.request_token = { 'oauth_token' : oauth_token, 'oauth_token_secret' : oauth_verifier } try: auth.get_access_token(oauth_verifier) add_user_field(user["user"], "twitter.access_token", auth.access_token) add_user_field(user["user"], "twitter.access_token_secret", auth.access_token_secret) log.warning(auth.access_token) log.warning(auth.access_token_secret) return "Twitter authentication successful" except tweepy.TweepError: log.warning(tweepy.TweepError) return 'Error! Failed to get access token.' return 'Error! Failed to get access token, request token not found'
def get_request_uri(username, service_username): # Use the client_secret.json file to identify the application requesting # authorization. The client ID (from that file) and access scopes are required. # Indicate where the API server will redirect the user after the user completes # the authorization flow. The redirect URI is required. # Generate URL for request to Google's OAuth 2.0 server. # Use kwargs to set optional request parameters. authorization_url, state = flow.authorization_url( # Enable offline access so that you can refresh an access token without # re-prompting the user for permission. Recommended for web server apps. access_type='offline', # Enable incremental authorization. Recommended as a best practice. include_granted_scopes='true') service_username = service_username + "@gmail.com" gmail_dict = {"state": state, "user": service_username} add_user_field(username, "gmail", gmail_dict) log.warning(authorization_url) return '<a href="' + authorization_url + '">Connect with Gmail</a>'
def play(username, parameters): user = get_user_by_field("user", username) if (user != None): devices = get_user_devices(user["user"]) add_user_field(user["user"], "spotify.devices", devices) if (devices["devices"][0]["name"] == parameters["MusicPlayerName"]): uri = "https://api.spotify.com/v1/me/player/play?device_id=" + devices[ "devices"][0]["id"] auth = "Bearer " + user["spotify"]["access_token"] + "" song = getSong(parameters["Song"], parameters["Artist"], parameters["Album"], user["spotify"]["access_token"]) payload = {"uris": [song]} headers = { "Accept": "application/json", "Content-Type": "application/json", "Authorization": auth } response = requests.put(uri, data=payload, headers=headers) log.warning(response.text) else: log.warning("Usuarios no coinciden") return ''