Youtube - viewing private videos when 'logged in' via API with owner's account
Is it possible to view your own private videos on a web page by logging in via the youtube API?
So if I have an authenticated session of some kind on the page, I can play my private video by doing something like: http://www.youtube.com/em开发者_如何学Gobed/gYD0lKSIwxY ?
This is how I change the status of a video. This allows me to change the private video to unlisted so I can view it from my site. I use a javascript timer and ajax to change it back to private AFTER it has started to play.
<?php
/**
* Change the status of a video on youtube
*/
if (isset($_POST['video_id'])) {
$video_id = stripslashes(strip_tags($_POST['video_id']));
$status = (isset($_POST['status'])) ? stripslashes(strip_tags($_POST['status'])) : 'private';
$sessionToken = 'your_session_token';
define('YT_DEVELOPER_KEY', 'your_developer_key');
$uploadURL = $uploadToken = null;
try {
$hCurl = curl_init();
if (!$hCurl)
throw new Exception('Failed to create cURL object');
$videoPrivate = <<<mof
<entry xmlns='http://www.w3.org/2005/Atom'
xmlns:media='http://search.yahoo.com/mrss/'
xmlns:yt='http://gdata.youtube.com/schemas/2007'
xmlns:gd='http://schemas.google.com/g/2005'
gd:fields='media:group/yt:private'>
<media:group>
<yt:private/>
</media:group>
</entry>
mof;
$videoUnlistedNotPrivate = <<<mof
<entry xmlns='http://www.w3.org/2005/Atom'
xmlns:yt='http://gdata.youtube.com/schemas/2007'
xmlns:gd='http://schemas.google.com/g/2005'
gd:fields='media:group(yt:private),yt:accessControl'>
<yt:accessControl action='comment' permission='allowed'/>
<yt:accessControl action='commentVote' permission='allowed'/>
<yt:accessControl action='embed' permission='allowed'/>
<yt:accessControl action='rate' permission='allowed'/>
<yt:accessControl action='list' permission='denied'/>
<yt:accessControl action='syndicate' permission='allowed'/>
<yt:accessControl action='videoRespond' permission='allowed'/>
</entry>
mof;
// send email for testing purposes
if ($status == 'private') {
mail('test@local.dev', 'VI test', 'setting private', 'from: test@local.dev');
$videoMeta = $videoPrivate;
} else {
$videoMeta = $videoUnlistedNotPrivate;
mail('test@local.dev', 'VI test', 'setting unlisted', 'from: test@local.dev');
}
$contentLength = strlen($videoMeta);
$respData = null;
$url = 'http://gdata.youtube.com/feeds/api/users/YourYouTubeName/uploads/' . $video_id;
$ops = array(
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_CUSTOMREQUEST => 'PATCH',
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_MAXREDIRS => 1,
CURLOPT_HTTPHEADER => array(
'Authorization: AuthSub token="' . $sessionToken . '"',
'GData-Version: 2',
'Content-Length: ' . $contentLength,
'Content-Type: application/xml',
'X-GData-Key: key=' . YT_DEVELOPER_KEY,
),
CURLOPT_POSTFIELDS => $videoMeta
);
curl_setopt_array($hCurl, $ops);
// fetch and parse the response to get upload URL and token
if (($respData = curl_exec($hCurl)) === false) {
throw new Exception('cURL returned an error while uploading video metadata: ' . curl_error($hCurl));
}
} catch (Exception $e) {
echo $e->getMessage();
exit;
}
}
?>
精彩评论