Padmanathan J
Padmanathan J

Reputation: 4620

Token invalid - Error 401 in YouTube API

I am Working send message using youtuba api. But i got a Error on my file. it shows Invalid Token 401 Error. My file is given below.I'm pretty sure I must be missing something vital but small enough to not notice it.

    <?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);
?>

any problem in my code? Please guide me. How can I get a result from this code?

Upvotes: 2

Views: 1262

Answers (1)

Ibrahim Ulukaya
Ibrahim Ulukaya

Reputation: 12877

Most likely your authentication is wrong, please debug that part first. Either you are not using right scope or that API is not enabled from your console.

On a separate note, I strongly suggest to use Youtube Data API v3 for this. We have updated PHP client library and great samples to get you started.

Upvotes: 1

Related Questions