Advertisement
Forecaster

Untitled

Mar 11th, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.52 KB | None | 0 0
  1. <?
  2. require_once 'Google/autoload.php';
  3. require_once 'Google/Client.php';
  4. require_once 'Google/Service/YouTube.php';
  5. require_once 'krumo/class.krumo.php';
  6.  
  7. $client_email = 'Service account email';
  8. $scopes = array(Google_Service_YouTube::YOUTUBE);
  9. $user_to_impersonate = 'The address of my YouTube account';
  10.  
  11. $private_key = file_get_contents('location of app .p12 file');
  12.  
  13. if (isset($user_to_impersonate) && $user_to_impersonate != "")
  14. {
  15.   $credentials = new Google_Auth_AssertionCredentials(
  16.       $client_email,
  17.       $scopes,
  18.       $private_key,
  19.       'notasecret',                                 // Default P12 password
  20.       'http://oauth.net/grant_type/jwt/1.0/bearer', // Default grant type
  21.       $user_to_impersonate
  22.   );
  23. }
  24. else
  25. {
  26.   $credentials = new Google_Auth_AssertionCredentials(
  27.       $client_email,
  28.       $scopes,
  29.       $private_key
  30.   );
  31. }
  32.  
  33. $client = new Google_Client();
  34. try
  35. {
  36.   $client->setAssertionCredentials($credentials);
  37. }
  38. catch (Google_Auth_Exception $e)
  39. {
  40.   die("false:Unable to acquire credentials.");
  41. }
  42.  
  43. if ($client->getAuth()->isAccessTokenExpired())
  44. {
  45.   try
  46.   {
  47.     $client->getAuth()->refreshTokenWithAssertion();
  48.   }
  49.   catch (Google_Auth_Exception $e)                        //<---- The code fails at this exception when the impersonate code is used
  50.   {
  51.     log_file($e->getMessage(), "../error.log");
  52.     die("false:Token has expired. Unable to refresh.");
  53.   }
  54. }
  55.  
  56. if (isset($_GET['display']))
  57. {
  58.   krumo($client);
  59. }
  60.  
  61. $youtube = new Google_Service_YouTube($client);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement