2

我正在使用 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);
?>

我的代码有什么问题吗?请指导我。如何从此代码中获得结果?

4

1 回答 1

1

很可能您的身份验证是错误的,请先调试该部分。您没有使用正确的范围,或者您的控制台未启用该 API。

另外,我强烈建议为此使用Youtube Data API v3。我们更新了PHP 客户端库出色的示例来帮助您入门。

于 2013-04-03T20:38:25.217 回答