Search code examples
zend-frameworkgoogle-apiyoutube-apioauth-2.0zend-gdata

Google OAuth and Zend connect using my application's credentials


My website allows users to upload videos to MY youtube account. To connect my aplication to Google (youtube) I used the component ClientLogin like this:

//my credentials
$user = '[email protected]';
$pass = 'mypass';
$service = 'youtube';
$developerKey = 'mydevkey';

//create the http client
$httpClient = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service, null,
                Zend_Gdata_ClientLogin::DEFAULT_SOURCE,null,null,
                Zend_Gdata_ClientLogin::CLIENTLOGIN_URI,'GOOGLE');
$httpClient->setHeaders('X-GData-Key', 'key='. $developerKey);

//create the instances
$youTubeService = new Zend_Gdata_YouTube($httpClient);
$newVideoEntry = new Zend_Gdata_YouTube_VideoEntry();
$newVideoEntry->setVideoTitle("test video");
$newVideoEntry->setVideoDescription("just testing");
$newVideoEntry->setVideoCategory("Music");
$newVideoEntry->setVideoTags('test, api');

//call the API to get the upload url and token
$tokenHandlerUrl = 'https://gdata.youtube.com/action/GetUploadToken';
try {
    $tokenArray = $youTubeService->getFormUploadToken($newVideoEntry, $tokenHandlerUrl);
} catch (Exception $e) {

}
$tokenValue = $tokenArray['token'];
$postUrl = $tokenArray['url'];

But now ClientLogin is deprecated :S, and I need to use oAuth 2... but have been reading the documentation and it says nothing about connecting using my apps credential (not the user's credentials). Is there a way to reproduce this code but using oAuth?


Solution

  • Theoretically, the way to do this with OAuth2 is with a service account:

    https://developers.google.com/accounts/docs/OAuth2ServiceAccount

    Once the account is set up in your API console, it then allows for server-to-server interactions. Unfortunately, the Youtube API doesn't yet support service accounts:

    https://code.google.com/p/google-api-php-client/wiki/OAuth2#Service_Accounts

    Hopefully this support will come soon (at the very least before ClientLogin goes away!)