I am using code examples of YouTube Api v3
- I turned on YouTube Data API v3.
- I create Oauth Client in the developer console
- I added my url (
$app->url('google.auth.test')
) to allowed redirect URI - I copied the client id and secret
Then I used example code
$client = new Google_Client(); $client->setClientId($OAUTH2_CLIENT_ID); $client->setClientSecret($OAUTH2_CLIENT_SECRET); $client->setApprovalPrompt('force');//read some topics - it could help $client->setAccessType('offline');//read some topics - it could help $client->setScopes('https://www.googleapis.com/auth/youtube'); $redirect = filter_var($app->url('google.auth.test'), FILTER_SANITIZE_URL);//uri of this page $client->setRedirectUri($redirect); // Define an object that will be used to make all API requests. $youtube = new Google_Service_YouTube($client); if (isset($_GET['code'])) { if (strval($_SESSION['state']) !== strval($_GET['state'])) { die('The session state did not match.'); } $cred = $client->authenticate($_GET['code']); dump($_GET['code']); dump($cred); dump($client->getAccessToken()); exit; $_SESSION['token'] = $client->getAccessToken(); header('Location: ' . $redirect); } if (isset($_SESSION['token'])) { $client->setAccessToken($_SESSION['token']); } // Check to ensure that the access token was successfully acquired. if ($client->getAccessToken()) { // This code creates a new, private playlist in the authorized user's //some code $_SESSION['token'] = $client->getAccessToken(); } else { // If the user hasn't authorized the app, initiate the OAuth flow $state = mt_rand(); $client->setState($state); $_SESSION['state'] = $state; $authUrl = $client->createAuthUrl(); $htmlBody = " <h3>Authorization Required</h3> <p>You need to <a href=\"{$authUrl}\">authorize access</a> before proceeding.<p> "; return $htmlBody; }
After I go to to $app-url('google.auth.test')
I am being redirected to
Authorization Required
You need to authorize access before proceeding.
It is OK.
After clicking - redirect to google auth it is OK.
Accept google auth and redirect back to
$app-url('google.auth.test')
I get code and my status in get request, but
$client->authenticate($_GET['code']);
return me errorDump info:
I think I did not find some advanced google settings.
Thanks for answers!