如何使用Youtube API的访问令牌获取用户上传的视频列表

时间:2015-09-28 11:53:18

标签: php api video youtube

我已经通过Oauth2获取了用户的访问令牌,因此在对用户进行身份验证并要求他们管理他们的Youtube数据后,我将访问令牌存储到数据库中

现在我想获取该经过身份验证的用户上传的视频的详细信息或列表,那么如何使用用户的访问令牌来获取他们的Youtube视频数据呢?

以下是我的代码

我生成认证网址

$url = "https://accounts.google.com/o/oauth2/auth";  
$params = array(
"response_type" => "code",
"client_id" => "XXXXXXX",
"redirect_uri" => "http://youtube-get.dev/oauth2callback.php",
"scope" => "https://gdata.youtube.com",
"access_type" => "offline"
);
$request_to = $url . '?' . http_build_query($params);
header("Location: " . $request_to);

通过在oauth2callback.php中调用curl,我得到了Access Token

$client_id="XXXXXXX";
$client_secret="XXXXXX";
$redirect_uri="http://youtube-get.dev/oauth2callback.php";

$oauth2token_url = "https://accounts.google.com/o/oauth2/token";
$clienttoken_post = array(
"code" => $code,
"client_id" => $client_id,
"client_secret" => $client_secret,
"redirect_uri" => $redirect_uri,
"grant_type" => "authorization_code"
);

$curl = curl_init($oauth2token_url);

curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $clienttoken_post);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$json_response = curl_exec($curl);
curl_close($curl);

$authObj = json_decode($json_response);

if (isset($authObj->refresh_token)){
    //refresh token only granted on first authorization for offline access
    //save to db for future use (db saving not included in example)
    global $refreshToken;
    $refreshToken = $authObj->refresh_token;
}

$accessToken = $authObj->access_token;

获取此Access令牌后如何使用它来获取经过身份验证的用户的视频列表?

所有的帮助将不胜感激,已经浪费了很多时间。

1 个答案:

答案 0 :(得分:0)

验证后使用以下网址获取验证用户视频

https://www.googleapis.com/youtube/v3/search?part=snippet&forMine=true&maxResults=25&q={your searchparameter}&type=video&key={your App Key}