You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flask应用部署服务器后遇Github API授权错误redirect_uri_mismatch求助

Github OAuth redirect_uri_mismatch 问题解决

核心问题定位

部署后出现的redirect_uri_mismatch错误,本质是Flask应用向Github发起授权请求时携带的redirect_uri参数,和你在Github OAuth Apps里注册的回调URL不一致。

具体修复步骤

1. 修正Github OAuth注册的URL

Amvera这类平台会自动处理HTTPS端口映射,容器内部的8080端口不需要暴露在外部URL中。你需要修改Github OAuth Apps的配置:

  • 首页URL改为:https://random-number-alandreamfall.amvera.io
  • 授权回调URL改为:https://random-number-alandreamfall.amvera.io/login/github/authorized

2. 配置Flask的URL相关参数

在Flask应用初始化时添加以下配置,确保生成的URL符合外部访问的HTTPS地址:

app = Flask(__name__)
# 信任反向代理传递的协议信息,识别外部HTTPS请求
app.config['TRUSTED_PROXIES'] = ['*']
# 指定外部访问的域名
app.config['SERVER_NAME'] = 'random-number-alandreamfall.amvera.io'
# 强制生成HTTPS格式的链接
app.config['PREFERRED_URL_SCHEME'] = 'https'

3. 强制指定OAuth回调地址

如果使用Flask-Dance等OAuth扩展,初始化时明确指定redirect_uri,避免自动生成的地址出错:

from flask_dance.contrib.github import make_github_blueprint, github

github_bp = make_github_blueprint(
    client_id="你的Client ID",
    client_secret="你的Client Secret",
    # 明确填写和Github注册完全一致的回调URL
    redirect_uri="https://random-number-alandreamfall.amvera.io/login/github/authorized",
)
app.register_blueprint(github_bp, url_prefix="/login")

4. 验证实际请求参数

可以在Flask中添加日志,查看生成的授权URL,确认redirect_uri参数是否正确:

@app.route('/', methods=['GET'])
def main():
    try:
        if not github.authorized:
            # 打印授权URL,检查其中的redirect_uri参数
            auth_url = url_for("github.login", _external=True)
            print(f"Authorization URL: {auth_url}")
            return redirect(auth_url)
        else:
            account_info = github.get("/user")
            if account_info.ok:
                return render_template('main.html')
    except Exception as e:
        print(f"An error occurred: ", e)

内容的提问来源于stack exchange,提问作者Alexander Azzazelius

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 04:35:25