我正在尝试从 html 表单中获取输入并为内容所有者显示 youtube 分析。我修改了以下示例:
https://developers.google.com/youtube/analytics/v1/sample-application
我正在使用以下范围
var OAUTH2_SCOPES = [
'https://www.googleapis.com/auth/yt-analytics.readonly',
'https://www.googleapis.com/auth/yt-analytics-monetary.readonly',
'https://www.googleapis.com/auth/youtube.readonly',
'https://www.googleapis.com/auth/youtubepartner'
];
getUserMetrics() 函数是从我的 html 表单执行的:
<div class="form">
Earnings and Ad Performance Metrics
<form name="metrics" action="form.asp">
CMS Name: <input type="text" name="ownerName"/><br />
Start date: <input type="date" name="startDate"/>
End date: <input type="date" name="endDate"/>
<input type="button" value="Submit" onclick="getUserMetrics(ownerName,startDate,endDate)"/>
</form>
function getUserMetrics(ownerName,startDate,endDate){
var owner = ownerName.value;
var start = startDate.value;
var end = endDate.value;
var request = gapi.client.youtubeAnalytics.reports.query({
'start-date': start,
'end-date': end,
ids: 'contentOwner=='+owner,
metrics : 'views,earnings,grossRevenue,playbackBasedCpm,monetizedPlaybacks,impressions,impressionBasedCpm',
filter: 'claimedStatus==claimed'
});
request.execute(function(response){
if("error" in response){
console.log(response);
}
else{
console.log("success");
}
});
这在 API Explorer 中运行良好: https ://developers.google.com/apis-explorer/#p/youtubeAnalytics/v1/youtubeAnalytics.reports.query
具有相同的指标、过滤器等。但是从我的表单运行它时,我收到错误:不支持查询。我最终想在 html 页面上显示结果,但似乎无法克服这个错误。