dougu1896 2017-03-28 06:54
浏览 77
已采纳

使用PHP谷歌客户端获取YouTube播放列表(服务器端oauth)

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 https://console.developers.google.com/apis/credentialsoauthclient/112609190871896620853?project=756606231401 to update the authorized redirect URIs.

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

<?php
require_once 'vendor/autoload.php';

session_start();

$client = new Google_Client();
$client->addScope('https://www.googleapis.com/auth/youtube');
$client->setAuthConfigFile('youtube-762b39a4f0b5.json');

if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
  $client->setAccessToken($_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';
  var_dump($redirect_uri);
  header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}

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

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

session_start();

$client = new Google_Client();
$client->setAuthConfigFile('client_secret.json');
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] .'/youtube/oauth2callback.php');
$client->addScope('https://www.googleapis.com/auth/youtube');

if (! isset($_GET['code'])) {
  $auth_url = $client->createAuthUrl();
  header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
  $client->authenticate($_GET['code']);
  $_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 2017-03-28 07: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 https://www.youtube.com/watch?v=hfWe1gPCnzc)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 指定IP电脑的访问设置
  • ¥30 matlab ode45 未发现警告,但是运行出错
  • ¥15 vscode platformio
  • ¥15 代写uni代码,app唤醒
  • ¥15 全志t113i启动qt应用程序提示internal error
  • ¥15 ensp可以看看嘛.
  • ¥80 51单片机C语言代码解决单片机为AT89C52是清翔单片机
  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
  • ¥15 在安装gym库的pygame时遇到问题,不知道如何解决
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部