因此,在阅读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连接,以了解该做什么,所以如果你会如此善良的解释给我做什么任何帮助将是受欢迎的。提前感谢大家的宝贵时间!
您可以使用报告。查询方法来执行此操作。它允许您检索许多不同的Analytics报告。每个请求使用查询参数来指定通道ID或内容所有者、开始日期、结束日期和至少一个度量。您还可以提供额外的查询参数,例如维度、过滤器或排序指令。
有一个例子,在提供的链接检索过去30天,但它是写在App脚本:
var analyticsResponse = YouTubeAnalytics.Reports.query(
'channel==' + channelId,
oneMonthAgoFormatted,
todayFormatted,
'views,likes,dislikes,shares',
{
dimensions: 'day',
sort: '-day'
});
要在PHP中做到这一点,我建议查看Youtube PHP代码示例。里面到处都是指南和说明。