2013-07-05 197 views
3

我们的项目允许用户在登录我们的会话时将视频上传到我们的公开YouTube频道。我们不想要求额外的OAuth2验证,而是遵循Google API v2的相关规定。Google YouTube API v3 ::服务器到服务器上传服务

我的代码(php)和错误如下,但我的一般问题是:我的服务器可以使用我的API密钥/秘密进行插入视频POST,而无需用户进行身份验证。

这个问题 - Google Calendar API v3 hardcoded credentials - 非常相似。不过如果CURLing的access_token是唯一的答案,我会感到失望。无论如何。

谷歌API V3设置:

Client ID: xxxx.apps.googleusercontent.com 
Email address: [email protected] 
Client secret: xxxx 
Redirect URIs: http://fbtabs.imagendigital.co/unilever/sedal/surprise/galeria 
JavaScript origins: https://fbtabs.imagendigital.co 
API key:  
AIzaSyBgS-jMJQUPIiAyX20xC-encFWTPsR7qxQ 
Referers: 
Any referer allowed 
Activated on: Jul 4, 2013 1:21 PM 
Activated by: [email protected] – you 

代码:

<?php 
require_once BASE_CD . 'app/src/Idframework/Tools.php'; 
require_once BASE_CD . 'app/vendor/google-api-php-client/src/Google_Client.php'; 
require_once BASE_CD . 'app/vendor/google-api-php-client/src/contrib/Google_YouTubeService.php'; 
$client = new Google_Client(); 
$client->setApplicationName('Google+ PHP Starter Application'); 
$client->setClientId('xxx.apps.googleusercontent.com'); 
$client->setClientSecret('xxxx'); 
$client->setRedirectUri('http://fbtabs.imagendigital.co/unilever/sedal/surprise/galeria'); 
$client->setDeveloperKey('xxxx'); 
$youtube = new Google_YoutubeService($client); 
$snippet = new Google_VideoSnippet(); 
$status = new Google_VideoStatus(); 
$video = new Google_Video(); 

if (isset($_POST['tip_desc'])) $snippet->setDescription($_POST['tip_desc']); 
    $snippet->setTitle($_POST['tip_name']); 
    $snippet->setTags(array("sedal","hair","pello","cabello")); 
    $status->privacyStatus = "public"; 

    $video->setSnippet($snippet); 
    $video->setStatus($status); 

    $filename = $_FILES['videoInp']['tmp_name']; 
    $mime = \Idframework\Tools::get_mime($filename); 

    try { 
     $part = "status"; 
     $obj = $youtube->videos->insert($part, $video, 
             array("data"=>file_get_contents($filename), 
             "mimeType" => $mime)); 
    } catch(Google_ServiceException $e) { 
     print "Caught Google service Exception ".$e->getCode(). " message is ".$e->getMessage(). " <br>"; 
     print "Stack trace is ".$e->getTraceAsString(); 
    } 

错误:

Notice: Undefined index: content-type in C:\DATA\IDInteractive\SedalSurprise\app\vendor\google-api-php-client\src\service\Google_MediaFileUpload.php on line 99 
Caught Google service Exception 401 message is Error calling POST https://www.googleapis.com/upload/youtube/v3/videos?part=player&uploadType=multipart&key=AIzaSyBgS-jMJQUPIiAyX20xC-encFWTPsR7qxQ: (401) Login Required 
Stack trace is #0 C:\DATA\IDInteractive\SedalSurprise\app\vendor\google-api-php-client\src\io\Google_REST.php(36): Google_REST::decodeHttpResponse(Object(Google_HttpRequest)) #1 C:\DATA\IDInteractive\SedalSurprise\app\vendor\google-api-php-client\src\service\Google_ServiceResource.php(186): Google_REST::execute(Object(Google_HttpRequest)) #2 C:\DATA\IDInteractive\SedalSurprise\app\vendor\google-api-php-client\src\contrib\Google_YouTubeService.php(789): Google_ServiceResource->__call('insert', Array) #3 C:\DATA\IDInteractive\SedalSurprise\youtuber.php(56): Google_VideosServiceResource->insert('player', Object(Google_Video), Array) #4 {main} 
+0

我解决了它,我的答案在这里: http://stackoverflow.com/questions/27609138/how-to-insert-video-youtube-api-v3-through-service-account-with-ruby/ 27620954#27620954 Enjoy! – dangalg

回答

3

首先,释放Walking不建议任意用户将视频上传到“主”YouTube频道,原因在于this blog post中列出的原因。这就是说,如果你决定这样做,那么你需要在每个上传请求中包含一个与你的频道相关的访问令牌。获取新访问令牌(在一小时后过期)的适当方式是获取先前生成并存储在某处的刷新令牌,并向相应的URL发出HTTP请求以获取访问令牌,如OAuth 2 documentation。这是没有办法的 - 我不确定在Google API PHP客户端库中是否有本地方法可以为您自动执行此过程,但这是需要在底层进行的。

Google Calendar API v3 hardcoded credentials中的信息确实很相关。

+0

翔实的答案! – JayKandari