Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Youtube API - Upload Videos on a Single Account?

I want to allow anyone register on my site, to upload their videos on my own youtube user channel.

I don't want them to comment any videos, or anything that requires their own login credentials.

Should I use: ClientLogin authorization ?

If so, how can I get a token so that I can allow my site to interact with my youtube channel account?

Any lights here will be greatly appreciated, since I'm kinda lost here.

like image 442
MEM Avatar asked Nov 23 '10 01:11

MEM


People also ask

Can I upload video to YouTube with API?

The programming language you use does not matter, the quota will be the same. So no there is no other way of uploading videos to YouTube using the api. You should just request a quota extension if you need to upload more videos.

How much video can I upload on YouTube using API?

Uploads a video to YouTube and optionally sets the video's metadata. This method supports media upload. Uploaded files must conform to these constraints: Maximum file size: 256GB.


1 Answers

I have accomplished this using ClientLogin. A basic class is below. This class returns an instance of Zend HTTP Client that is ready to make authenticated requests.

<?php

class GoogleAuthenticator {

  public static function authenticate($logger) {
    $tokenObj = new Token();
    try {
      $token = $tokenObj->get($token_name);
      if(!empty($token)) {
        //load a new HTTP client with our token
        $logger->info('Using cached token: ' . $token);
        $httpClient = new Zend_Gdata_HttpClient();
        $httpClient->setConfig(array(
                'maxredirects'    => 0,
                'strictredirects' => true,
                'useragent' => 'uploader/v1' . ' Zend_Framework_Gdata/' . Zend_Version::VERSION
            )
        );
        $httpClient->setClientLoginToken($token);
        //attempt to use our token to make an authenticated request. If the token is invalid
        // an exception will be raised and we can catch this below
        $yt = new Zend_Gdata_YouTube($httpClient, 'uploader/v1', '', $youtube_api_key);
        $query = new Zend_Gdata_YouTube_VideoQuery();
        $query->setFeedType('top rated');
        $query->setMaxResults(1);
        $yt->getPlaylistListFeed(null, $query); //ignore the response!
      } else {    
        $logger->info('Generating new HTTP client');  
        // Need to create a brand new client+authentication
        $authenticationURL= 'https://www.google.com/youtube/accounts/ClientLogin';
        $httpClient = 
                Zend_Gdata_ClientLogin::getHttpClient(
                        $username = YOUTUBE_USERNAME_PROD,
                        $password = YOUTUBE_PASSWORD_PROD,
                        $service = 'youtube',
                        $client = null,
                        $source = 'uploader/v1', 
                        $loginToken = null,
                        $loginCaptcha = null,
                        $authenticationURL);

        // get the token so we can cache it for later
        $token = $httpClient->getClientLoginToken();
        $tokenObj->destroy($token_name);
        $tokenObj->insert($token, $token_name);
      }
      return $httpClient;

    }catch(Zend_Gdata_App_AuthException $e) {
      $tokenObj->destroy($token_name);
        die("Google Authentication error: " . $e->getMessage());
    }catch(Exception $e) {
      $tokenObj->destroy($token_name);
        die("General error: " . $e->getMessage());
    }
  } // authenticate()
} // GoogleAuthenticator

?>

You'll need to have these constants defined:

YOUTUBE_USERNAME_PROD
YOUTUBE_PASSWORD_PROD

Or modify the class to pass them in. The try/catch is needed because tokens can expire, so you need to a way to refresh them. Also, you need to make a dummy request to ensure the Token is valid even after you create it.

Keep in mind that YouTube (well, as of 2 years ago or so) prevented you from uploading a video more of than every 10 minutes, which makes your use-case pretty difficult. That is, you cannot allow multiple videos being uploaded on a single accounts behalf, more of than every 10 min. But YouTube might have lifted this since then. Good luck

like image 77
Cody Caughlan Avatar answered Sep 23 '22 18:09

Cody Caughlan