【发布时间】:2017-01-10 22:51:26
【问题描述】:
因此,在阅读了 youtube api 的文档后,我已经成功地在我的服务器端应用程序上为 youtube 帐户建立了 oauth 连接,但我未能找到有关如何使用我获得的数据获取频道视图的解决方案。 所以我的代码看起来像这样(PHP 部分):
class YouTube {
protected $token;
protected $params = [];
/**
* YouTube constructor.
*
* @param null $id
*/
public function __construct($id = null)
{
if ($id) {
$this->token = $this->getToken($id);
}
}
/**
* Get api authorization
*
* @return string
*/
public function getAuthorizationUrl()
{
// Make redirect
$this->params = [
'client_id' => '########',
'redirect_uri' => '######',
'scope' => 'https://gdata.youtube.com&',
'response_type'=> 'code&',
'access_type' => 'offline'
];
$redirect_url = 'https://accounts.google.com/o/oauth2/auth?' . http_build_query($this->params);
return $redirect_url;
}
/**
* Get API token and save account list to db
*
* @param $code
*
* @return \App\Models\DynamicDashboard\ThirdPartyAccounts
*/
public function getCallbackUrl($code)
{
// Grab the returned code and extract the access token.
$this->params = [
'code' => $code,
'client_id' => '####',
'client_secret' => '#####',
'redirect_uri' => '#######',
'grant_type' => 'authorization_code'
];
// Get access token
$command = 'curl --data "' . http_build_query($this->params) . '" https://accounts.google.com/o/oauth2/token';
exec($command, $resultToken);
$resultToken = json_decode($resultToken[0]);
// Do a request using the access token to get the list of accounts.
$command = 'curl -H "Authorization: Bearer ' . $resultToken->access_token . '" https://accounts.google.com/o/oauth2.json';
exec($command, $result);
$result = json_decode($result[0]);
// Save data to db
$account = new ThirdPartyAccounts();
$account->account_id = $result->identity->id;
$account->type = 'youtube';
$account->name = $result->identity->first_name . ' ' . $result->identity->last_name;
$account->extra_details = json_encode([
'access_token' => $resultToken->access_token,
'token_type' => $resultToken->token_type,
'expires_in' => $resultToken->expires_in,
'refresh_token' => $resultToken->refresh_token
]);
$account->save();
return $account;
}
/**
* Get API token
*
* @param $id
*
* @return mixed
*/
private function getToken($id)
{
$mainAccount = ThirdPartyAccounts::find($id);
$youtubeToken = json_decode($mainAccount->extra_details);
return $youtubeToken;
}
}
但是我不知道如何使用这些数据,我不知道如何编写获取过去 30 天的频道观看次数所需的方法(每天都有数据)。 对不起,但我真的对 api 连接了解不够,无法理解该怎么做,如果你愿意向我解释该怎么做,任何帮助都会受到欢迎。提前感谢大家的宝贵时间!
【问题讨论】:
标签: php laravel youtube youtube-api youtube-data-api