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 に答える