Zend Youtube API - 在单个帐户上传视频?

时间:2010-11-23 01:19:50

标签: php youtube-api zend-gdata

我想允许任何人在我的网站上注册,在我自己的YouTube用户频道上传他们的视频。

我不希望他们评论任何视频或任何需要他们自己的登录凭据的内容。

我应该使用:ClientLogin授权吗?

如果是这样,我如何获得令牌以便我可以允许我的网站与我的YouTube频道帐户进行互动?

这里的任何灯光都会非常感激,因为我有点迷失在这里。

2 个答案:

答案 0 :(得分:3)

我使用ClientLogin完成了这项工作。基本课程如下。此类返回Zend HTTP Client的一个实例,该实例已准备好进行经过身份验证的请求。

<?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

?>

您需要定义这些常量:

YOUTUBE_USERNAME_PROD
YOUTUBE_PASSWORD_PROD

或修改类以传递它们。需要try / catch,因为标记可能会过期,因此您需要一种方法来刷新它们。此外,您需要创建一个虚拟请求,以确保令牌即使在创建后也有效。

请注意,YouTube(嗯,截至2年前左右)阻止您上传视频超过每10分钟一次,这使您的用例非常困难。也就是说,您不能允许代表单个帐户上传多个视频,超过每10分钟一次。但从那以后,YouTube可能已经取消了这一点。祝你好运

答案 1 :(得分:0)

由于我在文档中没有找到任何完整的API V3解决方案,因此我一直在寻求互联网的解决方案。最后,我将Python示例移植到PHP,并为其他有相同问题的人写了一篇关于它的博客文章:

Uploading a video to youtube through api version 3 in PHP

此博文使用Youtube V3 api和OAuth2,因此您不必担心它被弃用。 V2中的所有其他功能(ClientLogin,AuthSub和OAuth 1.0)自2012年4月20日起全部弃用