首页 > 解决方案 > 带有 Laravel 的 YouTube Data API v3:重定向 URI 不匹配

问题描述

我确实设置了两个 Google 项目,一个用于localhost生产,另一个用于生产。本地项目 ( http://localhost) 按预期工作,但服务器 ( https://example.com) 上的相同代码引发以下错误:

array:2 [▼
  "error" => "redirect_uri_mismatch"
  "error_description" => "Bad Request"
]

我已经交叉验证了所有 URL 并正确设置它们;即使从控制台更新后,我也下载了新的 JSON 文件并等待了一天。还是同样的问题。

不知道出了什么问题;任何帮助,将不胜感激。

标签: phplaravelgoogle-oauthyoutube-data-api

解决方案


您的帖子暗示您正在开发服务器端 Web 应用程序

在这种情况下,您显示的错误表明您在 Google Developers Console 中为相应项目设置的重定向 URI 与您的应用在调用 API 时实际发送到 API的重定向 URI 不匹配:

$client = new Google_Client();
$client->setAuthConfig('client_secret.json');
$client->addScope(GOOGLE_SERVICE_YOUTUBE::YOUTUBE_FORCE_SSL);
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php');
// offline access will give you both an access and refresh token so that
// your app can refresh the access token without user interaction.
$client->setAccessType('offline');
// Using "consent" ensures that your application always receives a refresh token.
// If you are not using offline access, you can omit this.
$client->setApprovalPrompt("consent");
$client->setIncludeGrantedScopes(true);   // incremental auth

确保双方(Google Developers Console 中的配置设置和应用程序本身)就要使用的重定向 URI 达成一致。


推荐阅读