2013-07-05 196 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