I keep running into “Oauth Google Access blocked: This app’s request is invalid” with my python program is using Flask

This is one of many attempts to try and learn how to make a local program with Flask and run it from Google Cloud Shell. I have laid all the problem in the comments at the top of the program. I also have a html template include after the demonstration program.

# copied from https://github.com/piyush-singhal/oauth-python/blob/main/app.py
# this seems the simplest way to authenicate with Google 
# developed on local box and works well.  It pops a browser window open and lets me pick my Google account on my browser
# 
# deploy to Google Shell in the GCP
# I get the expected screen
#   Example of Google Login in Python Flask Application
#   You are not logged in, Click on the below link to sign in with google.
# I click the link and then i get the dreaded 
#   "Access blocked: This app’s request is invalid"
# Any help you can offer would be appreciated.
#
# here is my debug log (with my client-id channged to protect me)
# start the logging.debug
# Hello  line 74
# Hello  line 76
 # * Debugger is active!
 # * Debugger PIN: 127-742-122
# Hello  line 78
# Hello  line 80
# Hello  line 81  Google.base_url    == https://www.googleapis.com/
# Hello  line 82  user_info_endpoint == /oauth2/v2/userinfo
# 127.0.0.1 - - [15/Jul/2024 11:41:41] "GET /?authuser=0 HTTP/1.1" 200 -
# Hello  line 89
# Hello  line 91 my_url_for-->/login/google
# 127.0.0.1 - - [15/Jul/2024 11:42:30] "GET /login HTTP/1.1" 302 -
# client_id = 0000000000000-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com
# Generated new state V18oHjswxgqJML9VgmF7ujpaiLi6KD.
# state = V18oHjswxgqJML9VgmF7ujpaiLi6KD
# redirect URL = https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=0000000000000-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com&redirect_uri=http%3A%2F%2F127.0.0.1%3A5000%2Flogin%2Fgoogle%2Fauthorized&scope=profile+email&state=V18oHjswxgqJML9VgmF7ujpaiLi6KD&prompt=consent


import os
import sys
from flask import Flask, render_template, redirect, url_for
from flask_dance.contrib.google import make_google_blueprint, google
import logging
import json
import json as json_module

app = Flask(__name__)

# Configuration
with open('./credentials.json', 'r') as file:
    json = json.load(file)
client_id     = json['installed']['client_id']
client_secret = json['installed']['client_secret']
secret_key    = os.environ.get("SECRET_KEY") or os.urandom(24)

logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, format='%(message)s')

print("start the logging.debug")
logging.debug("Hello  line 52")

app.config['DEBUG'] = True
app.config['SECRET_KEY'] = secret_key

logging.debug("Hello  line 57")

os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
os.environ['OAUTHLIB_RELAX_TOKEN_SCOPE'] = '1'

blueprint = make_google_blueprint(
    client_id=client_id,
    client_secret=client_secret,
    reprompt_consent=True,
    scope=["profile", "email"]
)
app.register_blueprint(blueprint, url_prefix="/login")

@app.route("/")
def index():
    google_data = None
    user_info_endpoint = '/oauth2/v2/userinfo'
    logging.debug("Hello  line 74")
    if google.authorized:
        logging.debug("Hello  line 76")
        google_data = google.get(user_info_endpoint).json()
        logging.debug("Hello  line 78")

    logging.debug("Hello  line 80")
    logging.debug("Hello  line 81  Google.base_url    == " + google.base_url)
    logging.debug("Hello  line 82  user_info_endpoint == " + user_info_endpoint)
    return render_template('index.j2',
                           google_data=google_data,
                           fetch_url=google.base_url + user_info_endpoint)

@app.route('/login')
def login():
    logging.debug("Hello  line 89")
    my_url_for=url_for('google.login')
    logging.debug("Hello  line 91 my_url_for-->" + my_url_for )
    return redirect(my_url_for)
    #return redirect('/login/google')  #this works

if __name__ == "__main__":
    app.run()

here is index.j2 in the template folder

<!doctype html>
<html class="no-js" lang="">

<head>
  <title>Google Oauth 2.0 Login for python Flask application</title>
  <link rel="icon" href="/favicon.ico" type="image/x-icon">
</head>

<body>
  <h3>Example of Google Login in Python Flask Application</h3>
  {% if not google_data %}
    <p style="color:#333;">You are not logged in, Click on the below link to sign in with google.</p>
    <a href="/login" class="google-btn">
      <div class="google-icon-wrapper">
        <img class="google-icon" src="https://upload.wikimedia.org/wikipedia/commons/c/c1/Google_%22G%22_logo.svg"/>
      </div>
      <p class="btn-text"><b>Sign in with google</b></p>
    </a>
  {% else %}
    <p>Hi {{ google_data.name }}, [<strong>{{ google_data.email }}</strong>]. You have logged in successfully from your Google Account. Check your below details.</p>
  {% if google_data is not none %}
    <div class="detail">
      <p style="font-size: 15px; padding-bottom: 10px;">User info fetched from <strong>{{ fetch_url }}</strong></p>
      <table border='1'>
      {% for key, value in google_data.items() %}
        <tr><td>{{ key }}</td> <td><strong>{{ value }}</strong></td></tr>
      {% endfor %}
      </table>
    </div>
    <div> <img src="{{ google_data.picture }}" alt="User Picture">    </div>
  {% endif %}
  {% endif %}

</html>

I created it locally and tried it on Google Cloud Shell. I expected it authenticate to google and show my info. Instead i get the “Access blocked: This app’s request is invalid” page

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật