【发布时间】:2015-09-28 11:53:18
【问题描述】:
我已经通过 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,我得到了访问令牌
$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;
获得此访问令牌后,我如何使用它来获取经过身份验证的用户的视频列表?
所有的帮助将不胜感激,已经浪费了很多时间。
【问题讨论】:
-
请向我们展示您当前的一些工作,并指出其中的问题所在,以及预期的行为和MCVE。
-
好的,我已经包含了我的代码,无论我到目前为止做了什么