For credentials, I have created an developer account on https://console.developers.google.com, I have created a project and then i have created credentials from API Manager. I use "google/apiclient": "1.1.*" package. I think it is a problem with credentials.
$OAUTH2_CLIENT_ID = 'XXXXX-rvm1l9b1nvht9je1ic0bbe05ab5gvhbg.apps.googleusercontent.com';
$OAUTH2_CLIENT_SECRET = 'XXXXXXP90L_DLD3Nrc_rT4zGD';
$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->setScopes('https://www.googleapis.com/auth/youtube');
$redirect = url('/');
$client->setRedirectUri($redirect);
$token = $client->getAccessToken();
dd($token);
I think the problem is you're not making the request to Google to authenticate and get back the token. You should do:
$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->setScopes('https://www.googleapis.com/auth/youtube');
$redirect = url('/');
$client->setRedirectUri($redirect);
//redirect to google server to get the token
return Redirect::to( $client->createAuthUrl() );
If the authentication succeeds, google will redirect you to the page you set with $client->setRedirectUri($redirect)
.
In that page you can:
//authenticate using the parameter $_GET['code'] you got from google server
$client->authenticate( $request->input('code') );
//get the access token
$tokens = $client->getAccessToken();
Tip:
Don't use:
$client->authenticate($authcode);
$token = $client->getAccessToken();
Use:
$token = $client->fetchAccessTokenWithAuthCode($authcode);
This will at least allow you to see the error message if you dump $token
. In the 'Don't use:' example, if the authenticate()
step fails, getAccessToken()
may return null. Bad job on the API here.