Streamlit 应用程序中的 Google Auth 错误“访问被阻止:授权错误”

问题描述 投票:0回答:1

我正在开发一个 Streamlit 应用程序,它使用 OAuth 2.0 与 Google Sheets 集成进行身份验证。然而,我一次又一次地遇到

Error 400: invalid_request
,指出在尝试进行身份验证时缺少必需的参数
redirect_uri
错误截图

我已检查我的 Google Cloud Console 设置和应用程序代码,但未能解决该问题。以下是我的代码和设置的相关部分:

from google_auth_oauthlib.flow import InstalledAppFlow
import streamlit as st

def authenticate_google():
    client_config = {
        "web": {
            "client_id": st.secrets["google_oauth"]["client_id"],
            "project_id": st.secrets["google_oauth"]["project_id"],
            "auth_uri": st.secrets["google_oauth"]["auth_uri"],
            "token_uri": st.secrets["google_oauth"]["token_uri"],
            "auth_provider_x509_cert_url": st.secrets["google_oauth"]["auth_provider_x509_cert_url"],
            "client_secret": st.secrets["google_oauth"]["client_secret"],
            "redirect_uris": st.secrets["google_oauth"]["redirect_uris"],
            "javascript_origins": st.secrets["google_oauth"]["javascript_origins"]
        }
    }
    flow = InstalledAppFlow.from_client_config(
        client_config, 
        scopes=['https://www.googleapis.com/auth/spreadsheets.readonly']
    )
    auth_url, _ = flow.authorization_url(prompt='consent')
    return auth_url

-- 我的

secrets.toml
中的 OAuth redirect_uris 配置:

[google_oauth]
redirect_uris = ["http://localhost:8501", "https://your-app-name.streamlitapp.com"]

--

我尝试过但未成功的修复:

  • 确保在Google Cloud Console中正确配置redirect_uri,与secrets.toml中的配置相匹配。
  • 检查了authorization_url()调用中使用的redirect_uri是否与Google Cloud Console中注册的一致。
  • 尝试删除代码中的显式redirect_uri设置以依赖于通过secrets.toml进行的配置。

我的问题是:

  1. 尽管在 OAuth 客户端设置中进行了配置,但可能会导致 redirect_uri 参数丢失的原因是什么?
  2. 是否有我可能忽略的特定格式或配置可能导致此问题?
python-3.x google-cloud-platform google-oauth streamlit
1个回答
-1
投票

IBM 1944。谷歌 2024。停止 Nimbus 项目。

© www.soinside.com 2019 - 2024. All rights reserved.