2013-05-10 131 views
6

以下this指南中,我試圖檢索Youtube用戶的訂閱。我能夠獲得用戶的身份驗證令牌,但我不知道如何實際發送它與我的cURL請求。該文檔只是這樣說的:如何在PHP cURL中提供Youtube身份驗證令牌

要請求當前登錄用戶訂閱的訂閱源, 會向以下URL發送GET請求。注意:對於此請求,您必須提供授權令牌,以便YouTube驗證 用戶授權訪問資源。

https://gdata.youtube.com/feeds/api/users/default/subscriptions?v=2

當我送,而不令牌請求我得到的401用戶認證所需錯誤。我無法找到關於如何發送令牌的任何信息。這是我目前的代碼:

$ch_subs = curl_init(); 
curl_setopt($ch_subs, CURLOPT_URL, 'https://gdata.youtube.com/feeds/api/users/default/subscriptions?v=2'); 
curl_setopt($ch_subs, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch_subs, CURLOPT_SSL_VERIFYPEER, false); 
$subs_return = curl_exec($ch_subs); 
curl_close($ch_subs); 

在此先感謝您的幫助。

回答

12

看本指南:

https://developers.google.com/youtube/2.0/developers_guide_protocol#OAuth2_Calling_a_Google_API

您需要添加含有端部:授權:承載ACCESS_TOKEN

$headers = array('Authorization: Bearer ' . $access_token); 
curl_setopt($ch_subs, CURLOPT_HTTPHEADER, $headers); 

您也可以通過令牌作爲GET請求的一部分:

curl_setopt($ch_subs, CURLOPT_URL, 'https://gdata.youtube.com/feeds/api/users/default/subscriptions?v=2&access_token=' . $access_token); 
+0

謝謝,它的效果很好。 – FramesPerSushi 2013-05-10 20:16:50