Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?
- require_once 'Google/autoload.php';
- require_once 'Google/Client.php';
- require_once 'Google/Service/YouTube.php';
- require_once 'krumo/class.krumo.php';
- $client_email = 'Service account email';
- $scopes = array(Google_Service_YouTube::YOUTUBE);
- $user_to_impersonate = 'The address of my YouTube account';
- $private_key = file_get_contents('location of app .p12 file');
- if (isset($user_to_impersonate) && $user_to_impersonate != "")
- {
- $credentials = new Google_Auth_AssertionCredentials(
- $client_email,
- $scopes,
- $private_key,
- 'notasecret', // Default P12 password
- 'http://oauth.net/grant_type/jwt/1.0/bearer', // Default grant type
- $user_to_impersonate
- );
- }
- else
- {
- $credentials = new Google_Auth_AssertionCredentials(
- $client_email,
- $scopes,
- $private_key
- );
- }
- $client = new Google_Client();
- try
- {
- $client->setAssertionCredentials($credentials);
- }
- catch (Google_Auth_Exception $e)
- {
- die("false:Unable to acquire credentials.");
- }
- if ($client->getAuth()->isAccessTokenExpired())
- {
- try
- {
- $client->getAuth()->refreshTokenWithAssertion();
- }
- catch (Google_Auth_Exception $e) //<---- The code fails at this exception when the impersonate code is used
- {
- log_file($e->getMessage(), "../error.log");
- die("false:Token has expired. Unable to refresh.");
- }
- }
- if (isset($_GET['display']))
- {
- krumo($client);
- }
- $youtube = new Google_Service_YouTube($client);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement