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

发布于 2024-10-03 17:16:04 字数 209 浏览 6 评论 0原文

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

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

我应该使用:ClientLogin 授权吗?

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

权谋诡计 2024-10-10 17:16:04

我已经使用 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 可能已经取消了这一规定。祝你好运

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

〗斷ホ乔殘χμё〖 2024-10-10 17:16:04

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

通过 PHP 中的 api 版本 3 将视频上传到 YouTube

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文