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?