def oauth2callback(): state = flask.session['state'] flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(CLIENT_SECRETS_FILE, state=state, scopes=SCOPES) flow.redirect_uri = flask.url_for('oauth2callback', _external=True) authorization_response = flask.request.url flow.fetch_token(authorization_response=authorization_response) credentials = dict_manager.Credentials.json_to_dictionary(flow.credentials.to_json()) token = credentials['token'] refresh = credentials['refresh_token'] scopes = credentials['scopes'] user_information = portal_requests.Launch.get_profile(token) unique_id = user_information['id'] email = user_information['email'] returning_user = User.get(unique_id) if not returning_user: registration, billing = check_status(token) User.create(unique_id, email, registration, billing, token, refresh, scopes) else: if not returning_user.registered == 200 or not returning_user.billable == 200: registration, billing = check_status(token) User.update_status(unique_id, registration, billing) User.update_tokens(unique_id, token, refresh, scopes) current_user = User.get(unique_id) flask_login.login_user(current_user) flask.flash('Logged in successfully') return flask.redirect(flask.url_for('index'))
def setUp(self): password = generate_password_hash("admin", method='sha256') user1 = User(None, "admin", password, "*****@*****.**", 76) user1.create()
def callback(): # Get authorization code Google sent back to you code = request.args.get("code") # Get token endpoint google_provider_cfg = get_google_provider_cfg() token_endpoint = google_provider_cfg["token_endpoint"] # Prepare and send a request to get tokens! Yay tokens! token_url, headers, body = client.prepare_token_request( token_endpoint, authorization_response=request.url, redirect_url=request.base_url, code=code) token_response = requests.post( token_url, headers=headers, data=body, auth=(GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET), ) # Parse the tokens! client.parse_request_body_response(json.dumps(token_response.json())) userinfo_endpoint = google_provider_cfg["userinfo_endpoint"] uri, headers, body = client.add_token(userinfo_endpoint) userinfo_response = requests.get(uri, headers=headers, data=body) # You want to make sure their email is verified. # The user authenticated with Google, authorized your # app, and now you've verified their email through Google! if userinfo_response.json().get("email_verified"): unique_id = userinfo_response.json()["sub"] users_email = userinfo_response.json()["email"] picture = userinfo_response.json()["picture"] users_name = userinfo_response.json()["given_name"] else: return "User email not available or not verified by Google.", 400 # Create a user in your db with the information provided # by Google user = User(id_=unique_id, name=users_name, email=users_email, profile_pic=picture) # Doesn't exist? Add it to the database. if not User.get(unique_id): User.create(unique_id, users_name, users_email, picture) # Begin user session by logging the user in login_user(user) # Get playlists (50 is the max results that can be returned) yt_uri, yt_headers, yt_body = client.add_token( "https://www.googleapis.com/youtube/v3/playlists?part=snippet&mine=true&maxResults=50" ) yt_playlist_response = requests.get(yt_uri, headers=yt_headers, data=yt_body) # print(yt_playlist_response.json()) playlists = yt_playlist_response.json()["items"] # get the rest of the playlists if there are any while "nextPageToken" in yt_playlist_response.json(): next_page_token = yt_playlist_response.json()["nextPageToken"] yt_uri, yt_headers, yt_body = client.add_token( "https://www.googleapis.com/youtube/v3/playlists?part=snippet&mine=true&maxResults=50&pageToken=" + next_page_token) yt_playlist_response = requests.get(yt_uri, headers=yt_headers, data=yt_body) playlists = playlists + yt_playlist_response.json()["items"] for playlist in playlists: playlist_id = playlist["id"] playlist_name = playlist["snippet"]["title"] playlist_thumbnail = playlist["snippet"]["thumbnails"]["high"]['url'] playlist_published_at = playlist["snippet"]["publishedAt"] # create a playlist object playlist_obj = Playlist(id_=playlist_id, name=playlist_name, thumbnail=playlist_thumbnail, published_at=playlist_published_at) # add playlist to local database if not Playlist.get(playlist_id): Playlist.create(playlist_id, playlist_name, playlist_thumbnail, playlist_published_at) # Send user back to homepage return redirect(url_for("index"))