我一直在使用Youtube Analytics API v2从youtube频道中检索信息,当我使用它从当前通过OAuth2.0进行身份验证的用户的频道中接收报告时,它一直在为我服务。代码在他们的网站上执行此操作,现在,我所需要的就是接收这些报告,而无需以已经拥有API所需权限的频道所有者的身份登录,该怎么办?
我已经看到我需要apiKey来执行此操作,我在代码中的每个位置都尝试了gapi.client.setApiKey(“ API_KEY”)代码,但没有得到任何结果,也许我我错过了一些东西,我从youtube Analytics网站上选择了示例代码,并将“ ids”参数从“ channel == MINE”更改为“ channel == PREVIOUSLY_AUTHORIZED_CHANNEL_ID”,现在当我尝试执行以下操作时会收到错误403(禁止)无需使用相应的Google帐户即可访问它。
<script src="https://apis.google.com/js/api.js"></script>
<script>
function authenticate() {
gapi.client.setApiKey("<API_KEY>");
return gapi.auth2.getAuthInstance()
.signIn({scope: "https://www.googleapis.com/auth/yt-analytics.readonly"})
.then(function() { console.log("Sign-in successful"); },
function(err) { console.error("Error signing in", err); });
}
function loadClient() {
return gapi.client.load("https://youtubeanalytics.googleapis.com/$discovery/rest?version=v2")
.then(function() { console.log("GAPI client loaded for API"); },
function(err) { console.error("Error loading GAPI client for API", err); });
}
// Make sure the client is loaded and sign-in is complete before calling this method.
function execute() {
return gapi.client.youtubeAnalytics.reports.query({
"ids": "channel==<PREVIOUSLY_AUTHORIZED_CHANNEL_ID>",
"startDate": "2017-01-01",
"endDate": "2017-12-31",
"metrics": "views,estimatedMinutesWatched,averageViewDuration,averageViewPercentage,subscribersGained",
"dimensions": "day",
"sort": "day"
})
.then(function(response) {
// Handle the results here (response.result has the parsed body).
console.log("Response", response);
},
function(err) { console.error("Execute error", err); });
}
gapi.load("client:auth2", function() {
gapi.auth2.init({client_id: '17857976174-df6g4g2kajnh8mpoebk0o10n4o7ddvb8.apps.googleusercontent.com'});
});
</script>
<button onclick="authenticate().then(loadClient)">authorize and load</button>
<button onclick="execute()">execute</button>
答案 0 :(得分:1)
现在我所需要的只是在不经过频道所有者身份验证的情况下接收这些报告,该怎么办?
你不能。 YouTube分析数据是私人用户数据。私有数据需要用户同意才能访问。您必须要获得用户的同意才能访问。
API密钥用于访问公共数据。像YouTube一样搜索公共的youtube视频。一些公共Google日历和公共Google驱动器文件。