我在获取带有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();
}
我只使用刷新令牌来为每个请求提供一个新的访问令牌,这对于使用所有其他维度的报表都非常有效,特别是除了这个维度。
查询此维度是否有其他特殊要求?
我很感谢你的帮助。
谢谢。
发布于 2020-04-28 16:44:24
https://stackoverflow.com/questions/60650079
复制相似问题