2012-10-01 20 views
0

我試圖使用Zend_Gdata(Zend Framework 1.12.0)通過API將視頻上傳到YouTube。我沒有直接上傳工作的問題,但基於瀏覽器的上傳總是給我一個400 - INVALID TOKEN錯誤。我很確定我必須錯過一些至關重要但足夠小而不會注意到的事情。上傳到YouTube時獲取TOKEN_INVALID

有兩個文件參與此:

的index.php

<?php 
$youTubeAPIKey = '<API_Key>'; 
$username = '<user>'; 
$password = '<pass>'; 

set_include_path(get_include_path().PATH_SEPARATOR.__DIR__."/vendor"); 
require_once 'Zend/Loader/Autoloader.php'; 
Zend_Loader_Autoloader::getInstance(); 

try 
{ 
    $authenticationURL= 'https://www.google.com/accounts/ClientLogin'; 
    $httpClient = Zend_Gdata_ClientLogin::getHttpClient(
       $username, 
       $password, 
       $service = 'youtube', 
       $client = null, 
       $source = 'BrowserUploaderTest', // a short string identifying your application 
       $loginToken = null, 
       $loginCaptcha = null, 
       $authenticationURL); 

    $yt = new Zend_Gdata_YouTube($httpClient, "browser upload test", "Test version 0.1", $youTubeAPIKey); 
    $videoEntry = new Zend_Gdata_YouTube_VideoEntry(); 

    $videoEntry->setVideoTitle("Test movie"); 
    $videoEntry->setVideoDescription("This is a test movie"); 
    $videoEntry->setVideoPrivate(); 

    // @todo This must be a valid YouTube category, how to get a list of valid categories? 
    $videoEntry->setVideoCategory('Autos'); 
    $videoEntry->setVideoTags('cars, funny'); 

    // Get an upload token 
    $tokenHandlerUrl = 'http://gdata.youtube.com/action/GetUploadToken'; 
    $tokenArray = $yt->getFormUploadToken($videoEntry, $tokenHandlerUrl); 
    $token = $tokenArray['token']; 
    $url = $tokenArray['url']; 
    // print "Token value: {$tokenArray['token']}\n url: {$tokenArray['url']}\n"; 
    $nextUrl = "http://" . $_SERVER['HTTP_HOST'] . "/uploadDone.php"; 
} 
catch (Zend_Gdata_App_HttpException $httpException) 
{ 
    echo $httpException->getRawResponseBody(); 
} 
catch (Zend_Gdata_App_Exception $e) { 
    echo $e->getMessage(); 
} 
catch (Exception $e) 
{ 
    print $e->getTraceAsString(); 
} 
?><!DOCTYPE html> 
<html> 
    <head> 
     <title>Testing Youtube upload</title> 
    </head> 

    <body> 
     <table> 
      <tr> 
       <td> 
        Url: 
       </td> 
       <td> 
        <?= $url ?> 
       </td> 
      </tr> 
      <tr> 
       <td> 
        Token: 
       </td> 
       <td> 
        <?= $token ?> 
       </td> 
      </tr> 
     </table> 
     <form action="<?= $url ?>.?nexturl=<?= urlencode($nextUrl) ?>" enctype="multipart/form-data" method="post"> 
      <input name="token" type="hidden" value="<?= $token ?>" /> 
      <input name="file" type="file" /> 
      <input type="submit" value="Upload file" /> 
     </form> 
    </body> 
</html> 

uploadDone.php

<?php 
print nl2br(print_r($_GET, true)); 
print nl2br(print_r($_POST, true)); 

我搜索都在堆棧溢出或花了幾個小時在谷歌搜索,但沒有找到任何解決它的事情導致我相信我錯過了一些簡單的事情。任何幫助,將不勝感激。

的說明: 此代碼只是爲了測試API的使用,並採取大多來自谷歌的開發者指南(https://developers.google.com/youtube/2.0/developers_guide_php#Browser_based_Upload)和一點點來自Yii框架文檔的幫助(http://www.yiiframework.com/wiki/375/youtube-api-v2-0-browser-based-uploading/)。生產代碼將以更結構化的方式進行重寫,但目前並不重要。

+0

您是否正在使用沒有關聯YouTube頻道的「未關聯」Google帳戶進行測試?有更多信息在http://apiblog.youtube.com/2011/10/introducing-google-account-support-and.html –

+0

對不起Jeff,完全錯過了您的評論。我使用的帳戶是專門爲此目的在YouTube網站上創建的帳戶,我希望這個帳戶足以與自己的頻道建立關聯帳戶。至少我可以直接從光盤上傳到帳戶,而不是通過網絡表單。 – h00ligan

+0

好吧,$ youtubeApiKey值如何?這是來自https://code.google.com/apis/youtube/dashboard/gwt/index.html嗎? –

回答

1

您的action="<?= $url ?>.?nexturl=<?= urlencode($nextUrl) ?>"看起來很可疑;是否有錯誤的.字符在你的$url變量被正確評估之後,搞亂了URL?

+0

這當然是它!我知道它必須是令人難以置信的簡單 - 謝謝! – h00ligan

相關問題