【发布时间】:2020-06-24 06:23:31
【问题描述】:
我在使用具有 adType 维度的 youtube 分析 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();
}
我只使用刷新令牌在每个请求上获得一个新的访问令牌,这非常适用于使用除此之外的所有其他维度的报告。
查询此维度是否有任何其他特殊要求?
感谢您的帮助。
谢谢。
【问题讨论】:
标签: youtube-api youtube-data-api