2013-04-02 46 views
2

我正在使用youtuba api工作發送消息。但是我的文件中出現錯誤。它顯示無效令牌401錯誤。我的文件在下面給出。我非常確定我必須錯過一些重要但小到不能注意到的東西。令牌無效 - YouTube API中的錯誤401

<?php 
$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL, "https://www.google.com/accounts/ClientLogin"); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 

$data = array('accountType' => 'GOOGLE', 
'Email' => 'User Email', 
'Passwd' => 'pass', 
'source'=>'PHI-cUrl-Example', 
'service'=>'lh2'); 

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
    $kk = curl_getinfo($ch); 
$response = curl_exec($ch); 

list($auth, $youtubeuser) = explode("\n", $response); 
list($authlabel, $authvalue) = array_map("trim", explode("=", $auth)); 
list($youtubeuserlabel, $youtubeuservalue) = array_map("trim", explode("=", $youtubeuser)); 


$developer_key = 'AI39si7SavL5-RUoR0kvGjd0h4mx9kH3ii6f39hcAFs3O1Gf15E_3YbGh-vTnL6mLFKmSmNJXOWcNxauP-0Zw41obCDrcGoZVw'; 
$token = '7zWKm-LZWm4'; //The user's authentication token 
$url = "http://gdata.youtube.com/feeds/api/users/worshipuk/inbox" ; //The URL I need to send the POST request to 
$title = $_REQUEST['title']; //The title of the caption track 
$lang = $_REQUEST['lang']; //The languageof the caption track 
$transcript = $_REQUEST['transcript']; //The caption file data 
$headers = array(
    'Host: gdata.youtube.com', 
    'Content-Type: application/atom+xml', 
    'Content-Language: ' . $lang, 
    'Slug: ' . rawurlencode($title), 
    'Authorization: GoogleLogin auth='.$authvalue, 
    'GData-Version: 2', 
    'X-GData-Key: key=' . $developer_key 
); 



$xml = '<?xml version="1.0" encoding="UTF-8"?> 
<entry xmlns="http://www.w3.org/2005/Atom" 
    xmlns:yt="http://gdata.youtube.com/schemas/2007"> 
    <id>Qm6znjThL2Y</id> 
    <summary>sending a message from the api</summary> 
</entry>'; 
// create a new cURL resource 
$ch = curl_init(); 

// set URL and other appropriate options 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_HEADER, TRUE); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode($xml)); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_VERBOSE, 1); 

$tt = curl_getinfo($ch); 
print_r($tt); 


$result = curl_exec($ch); 
print_r($result); 
exit; 

// close cURL resource, and free up system resources 
curl_close($ch); 
?> 

在我的代碼中的任何問題?請指導我。我如何從這段代碼中得到結果?

+0

我相信錯誤401是拒絕訪問,沒有太大的幫助,但這可能會給你一個地方開始:) –

+0

電子郵件'=>'用戶電子郵件','密碼'=>'通過',請把youtube電子郵件和密碼然後你繼續。 –

回答

1

很可能您的身份驗證錯誤,請先調試該部分。要麼你沒有使用正確的範圍,要麼你的控制檯沒有啓用API。

另外,我強烈建議使用Youtube Data API v3這個。我們已更新PHP client librarygreat samples以幫助您入門。

+1

我的第一部分工作很好。 YouTube的正確迴應來自YouTube。 –

+1

請給我youtube.all樣品文件在zend框架工作。所以我很困惑。請幫助我 –