2011-07-28 104 views
2

我有一個Web應用程序,讓用戶登錄YouTube並授權我的Web應用程序訪問其帳戶。但是,我需要他們的YouTube用戶名來實際列出他們上傳的視頻,並且我想確保他們提供的YouTube用戶名與他們用來授權的帳戶相匹配。換句話說,我只希望用戶能夠分享他們上傳的視頻,而不是其他人的視頻。有沒有辦法做到這一點?Youtube PHP API:驗證YouTube用戶名與驗證令牌匹配

我以前用C#.NET用下面的代碼這樣做:

YouTubeRequestSettings yt_settings = new YouTubeRequestSettings(<devID name>, devKey, auth); 
YouTubeRequest yt_request = new YouTubeRequest(yt_settings); 

Uri uri = new Uri("http://gdata.youtube.com/feeds/api/users/default/uploads/?start-index=1&max-results=1"); 
Feed<Video> videoFeed = yt_request.Get<Video>(uri); 
string uploader = ""; 
if (videoFeed.Entries.Count() > 0) 
    uploader = videoFeed.Entries.ElementAt(0).Uploader; 

但是當我嘗試用PHP類似的東西,我得到什麼似乎是一個標準的飼料:

$yt = new Zend_Gdata_YouTube($http_client,<devID name>,null,$_yt_dev_key); 
$feed_url = urlencode("http://gdata.youtube.com/feeds/api/users/default/uploads/?start-index=1&max-results=1"); 
$videoFeed = $yt->getVideoFeed(); 
if(count($videoFeed) > 0) 
{ 
    $videoEntry = $videoFeed[0]; 
    echo var_dump($videoEntry); 
} 

任何人有任何想法,如果我做錯了什麼?

------------------ UPDATE ----------------------

I有解決方案可以在經過身份驗證的會話中爲用戶提供YouTube視頻Feed。 $ videoFeed = $ yt-> getuserFeed(「default」); 雖然,我仍然在尋找如何從這個獲得上傳者名稱,以便我可以直接從JavaScript執行更多的視頻列表(就像我用我的舊C#/ Asp .Net網絡應用程序完成的那樣)。

-----------一個相當粗糙的解決方案------------

好了,這不正是一個完美的解決方案,但它是我有工作。

下面將提取一個的VideoEntry對象的YouTube用戶名...

$videoFeed = $yt->getuserUploads("default"); 
if(count($videoFeed) > 0) 
{ 
    $videoEntry = $videoFeed[0]; 
    $v_dump = var_export($videoEntry, true); 
    $check_for = "http://gdata.youtube.com/feeds/api/users/"; 
    $pos = strpos($v_dump,$check_for); 
    $start_pos = $pos + strlen($check_for); 
    $user_name = ""; 
    for($i = $start_pos; $i < strlen($v_dump); $i++) 
    { 
     if(($v_dump[$i] == '/') or ($v_dump[$i] == '?')) 
      break; 
     $user_name .= $v_dump[$i]; 
    } 
    echo $user_name; 
} 

基本上,我在整個視頻entry變量字符串表示解析爲飼料URL的前序部分,那麼在作爲用戶名的url中獲取下一個標記。換句話說,我在定位這個網址在var轉儲和分析用戶名了:

http://gdata.youtube.com/feeds/api/users/<username>/uploads 

如果任何人都可以找到一個更好的和更清潔的方式來做到這一點,那簡直是真棒。像這樣解析一個大字符串似乎是這樣做的一個非常骯髒的方式。

回答

1

試試這個:

$user = $yt->getUserProfile('default'); 
$username = $user->getUserName(); 
+0

對不起,你可能需要用$ YT-第一火起來V2> setMajorProtocolVersion(2); – Patrick