【问题标题】:Token invalid - Error 401 in YouTube API令牌无效 - YouTube API 中出现错误 401
【发布时间】:2013-03-23 10:39:15
【问题描述】:

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

我的代码有什么问题吗?请指导我。我怎样才能从这段代码中得到结果?

【问题讨论】:

  • 我相信错误 401 是访问被拒绝,帮助不大,但这可能会给你一个开始的地方:)
  • Email' => 'User Email','Passwd' => 'pass',请输入youtube邮箱和密码,然后继续。

标签: php api youtube google-api youtube-api


【解决方案1】:

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

另外,我强烈建议为此使用Youtube Data API v3。我们已更新 PHP client librarygreat samples 以帮助您入门。

【讨论】:

  • 我的第一部分运行良好。正确的回应来自 youtube。
  • 请给我 youtube 上的任何示例文件。所有示例都在 zend 框架中。所以我很困惑。请帮助我
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-18
  • 1970-01-01
  • 2014-08-22
  • 1970-01-01
  • 2011-04-22
  • 2013-05-12
  • 1970-01-01
相关资源
最近更新 更多