def callback():
    # Get authorization code Google sent back to you
    code = request.args.get("code")
    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)

    if userinfo_response.json().get("email_verified"):
        user_data = userinfo_response.json()
        users_email = user_data["email"]
        picture = user_data["picture"]
        users_name = user_data["given_name"]
    else:
        return "User email not available or not verified by Google.", 400
    user = User(name=users_name, email=users_email, profile_pic=picture)

    # # Doesn't exist? Add it to the database.

    if not User.get(users_email):
        User.create(users_name, users_email, picture)

    # # Begin user session by logging the user in
    login_user(user)

    # Send user back to homepage
    return redirect(url_for("index"))
示例#2
0
def print_messages(uname):
    ''' Our method that will print out all the messages from a user '''
    user = User.get(username=uname)
    for msg in user.messages:
        print(msg.message)
def load_user(email):
    return User.get(email)
示例#4
0
def create_messages(username, message):
    ''' Our method where we will create message objects '''
    user_ref = User.get(User.username == username)
    msg = Mweet(user=user_ref, message=message, likes=0)
    msg.save()