【问题标题】:Authorization Error, Error 400: redirect_uri_mismatch Gmail API授权错误,错误 400:redirect_uri_mismatch Gmail API
【发布时间】:2021-01-23 10:30:28
【问题描述】:

我正在关注此 Ruby Gmail API quickstart guide 以授权用户使用 Gmail API。我在 Google 控制台上创建了一个 Web Application 类型的应用程序并生成了它的 credentials.json 文件(并将它放在 quickstart.rb 旁边)。我已经为在heroku 上作为rails 应用程序托管的应用程序提供了redirect_uri。网址格式如下

https://myapp-api-heroku.com/my_redirect_endpoint

当我运行 quickstart.rb 时,它会在控制台中显示以下消息

在浏览器中打开以下网址并输入结果代码 授权后: https://accounts.google.com/o/oauth2/auth?access_type=offline&approval_prompt=force&client_id=my_client_id_here&include_granted_scopes=true&redirect_uri=urn:ietf:wg:oauth:2.0:oob&response_type=code&scope=https://www.googleapis.com/auth/gmail.readonly

在浏览器中打开链接后会显示此错误

请求中的重定向 URI urn:ietf:wg:oauth:2.0:oob 只能由本地应用程序的客户端 ID 使用。 WEB 客户端类型不允许。你可以在https://console.developers.google.com/apis/credentials/oauthclient为原生应用创建一个客户端ID

我已经在相应应用程序的谷歌控制台上注册了提供的 redirect_uri。我找不到导致此问题的原因。我最终想从我的 rails 应用服务器调用此 Gmail API,但我无法继续进行。

【问题讨论】:

    标签: ruby google-api gmail-api google-developers-console google-api-ruby-client


    【解决方案1】:

    有几种类型的身份验证让我们看一下其中的两种。第一个是 web,它被设计为在 web 服务器上工作,授权返回给 webserver 本身来处理它。第二个是已安装的应用程序,它会将响应返回给本地主机或发送请求的端点。

    您关注的代码Ruby quick start 在顶部声明它被设计为控制台应用程序或已安装的应用程序,这就是为什么您看到 urn:ietf:wg:oauth:2.0:oob,这意味着 localhost

    完成本页其余部分中描述的步骤,以创建一个简单的 Ruby 命令行应用程序,该应用程序向 Gmail API 发出请求。

    用于对两种不同类型的客户端进行身份验证的代码是不同的。您示例中的代码旨在与已安装的应用程序一起使用,以便它工作您需要在谷歌开发者控制台上创建已安装的本机凭据您已创建的 Web 凭据不适用于您正在使用的代码。

    对于 Web 服务器应用程序,您应该关注 webauth

    require 'google/apis/drive_v2'
    require 'google/api_client/client_secrets'
    
    client_secrets = Google::APIClient::ClientSecrets.load
    auth_client = client_secrets.to_authorization
    auth_client.update!(
      :scope => 'https://www.googleapis.com/auth/drive.metadata.readonly',
      :redirect_uri => 'http://www.example.com/oauth2callback',
      :additional_parameters => {
        "access_type" => "offline",         # offline access
        "include_granted_scopes" => "true"  # incremental auth
      }
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-14
      • 1970-01-01
      • 1970-01-01
      • 2021-11-05
      • 2020-10-04
      • 2018-09-06
      • 1970-01-01
      • 2021-01-05
      相关资源
      最近更新 更多