2017-03-28 14:54 阅读 69


I am using the PHP Google_Client to get the youtube playlist data on my web (a server-side Oauth ,without user check ) but get Error: redirect_uri_mismatch and message

The redirect URI in the request, http://localhost/youtube/oauth2callback.php, does not match the ones authorized for the OAuth client. Visit to update the authorized redirect URIs.

here is the http://localhost/youtube/index.php code

require_once 'vendor/autoload.php';


$client = new Google_Client();

if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
  $youtube = new Google_Service_YouTube($client);
  $playlists = $youtube->playlists->listPlaylists("snippet,status", array(
      'channelId' => 'UCBbOgoYXQdR-LRqrx7hdd6g'
  echo json_encode($playlists->toSimpleObject());
} else {

  $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/youtube/oauth2callback.php';
  header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));

here is the http://localhost/youtube/oauth2callback.php

require_once __DIR__.'/vendor/autoload.php';


$client = new Google_Client();
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] .'/youtube/oauth2callback.php');

if (! isset($_GET['code'])) {
  $auth_url = $client->createAuthUrl();
  header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
  $_SESSION['access_token'] = $client->getAccessToken();

  $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] .  '/youtube/index.php';
  header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));

Any idea?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    douyi5961 douyi5961 2017-03-28 15:46

    Looking at the screenshots, you have created a Service Account. Service Accounts use two-legged OAuth, and therefore do not have any user-consent-and-redirect dance.

    A Service Account would not have permissions to videos in your personal Gmail account. Maybe that's OK, maybe it isn't. It depends on your use case which you don't describe in your question. If you need your application to act with the same permissions as your Gmail account, you'll need to use a slightly different technique. (described How do I authorise an app (web or installed) without user intervention? (canonical ?) and

    点赞 评论 复制链接分享