MEM*_*MEM 9 php youtube-api zend-gdata
我想允许任何人在我的网站上注册,在我自己的YouTube用户频道上传他们的视频.
我不希望他们评论任何视频或任何需要他们自己的登录凭据的内容.
我应该使用:ClientLogin授权吗?
如果是这样,我如何获得令牌以便我可以允许我的网站与我的YouTube频道帐户进行互动?
这里的任何灯都会非常感激,因为我有点迷失在这里.
我已经使用 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
?>
Run Code Online (Sandbox Code Playgroud)
您需要定义这些常量:
YOUTUBE_USERNAME_PROD
YOUTUBE_PASSWORD_PROD
Run Code Online (Sandbox Code Playgroud)
或者修改类以将它们传入。需要 try/catch 因为令牌可能会过期,所以您需要一种刷新它们的方法。此外,您还需要发出一个虚拟请求,以确保令牌即使在创建后仍然有效。
请记住,YouTube(嗯,大约 2 年前)阻止您上传视频的时间超过每 10 分钟,这使您的用例变得非常困难。也就是说,您不能允许代表单个帐户上传多个视频,且上传次数不得超过每 10 分钟一次。但自那以后 YouTube 可能已经取消了这一规定。祝你好运