我在使用带有adType维度的youtube Analytics API获取广告效果报告时遇到问题,我收到以下响应:
{
"code" : 403,
"errors" : [ {
"domain" : "global",
"message" : "Forbidden",
"reason" : "forbidden"
} ],
"message" : "Forbidden"
}
根据文档(https://developers.google.com/youtube/analytics/channel_reports#ad-performance-reports中的指定,我生成了示例中指定的范围的凭证:
List<String> scopes = Lists.newArrayList(
"https://www.googleapis.com/auth/yt-analytics-monetary.readonly",
"https://www.googleapis.com/auth/yt-analytics.readonly",
"https://www.googleapis.com/auth/youtube.readonly"
);
// Authorize the request.
Credential credential = Auth.authorize(scopes, "analyticsreports");
然后这就是我要求报告的方式:
public QueryResponse getAdPerformaceReportByVideo(String channelId, String videoId, String startDate, String endDate) throws IOException {
UserCredentials userCredentials = buildUserCredentials();
userCredentials.refresh();
final HttpRequestInitializer httpRequestInitializer = new HttpCredentialsAdapter(userCredentials);
final HttpTransport httpTransport = new NetHttpTransport();
final JsonFactory jsonFactory = new JacksonFactory();
YouTubeAnalytics youTubeAnalytics = new YouTubeAnalytics.Builder(httpTransport, jsonFactory, httpRequestInitializer)
.setApplicationName("myapp")
.build();
final YouTubeAnalytics.Reports.Query request = youTubeAnalytics.reports().query();
try {
final QueryResponse queryResponse = request.setStartDate(startDate)
.setEndDate(endDate)
.setIds("channel==" + channelId)
.setMetrics("grossRevenue,adImpressions,cpm")
.setDimensions("adType,day")
.setFilters("video==" + videoId)
.execute();
return queryResponse;
} catch (IOException e) {
throw new YoutubeInsightsException("", e);
}
}
private UserCredentials buildUserCredentials() {
return UserCredentials.newBuilder()
.setClientSecret(this.clientSecret)
.setClientId(this.clientId)
.setRefreshToken(this.youtubeRefreshToken)
.build();
}
我只使用刷新令牌在每个请求上都具有一个新的访问令牌,这特别适用于使用所有其他维度的报表,尤其是此维度。
还有其他特殊要求来查询此维度吗?
感谢您的帮助。
谢谢。
我发现我们需要CMS帐户才能在此处检索此数据:How to use YouTube analytics monetary。
应该在API文档中指定。