开发者

Zend Youtube API - Upload Videos on a Single Account?

开发者 https://www.devze.com 2023-01-26 16:25 出处:网络
I want to allow anyone register on my site, to upload their videos on my own youtube user channel. 开发者_JS百科I don\'t want them to comment any videos, or anything that requires their own login cre

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

开发者_JS百科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.


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


Since I didn't find any complete solutions for API V3 in the documentation I've been exploring the Internet for a solution. In the end I ported the Python example to PHP and wrote a blog post about it for other people that have the same problem:

Uploading a video to youtube through api version 3 in PHP

This blogpost uses the Youtube V3 api with OAuth2 so you don't have to worry about it being deprecated. All other functions (ClientLogin, AuthSub and OAuth 1.0) in V2 are all deprecated as of April 20, 2012.

0

精彩评论

暂无评论...
验证码 换一张
取 消