com.google.api.client.googleapis.json.GoogleJsonResponseException:403禁止例外

时间:2017-02-21 05:29:54

标签: java google-analytics google-api google-analytics-api google-api-java-client

我在将分析数据作为源转发时发现了这个错误。

任何人都可以说这可能是什么...... ??

public Get getAnalyticsReportDefination() {
    String month = ((fromCal.get(Calendar.MONTH) + 1) < 10 ? "0" + (fromCal.get(Calendar.MONTH) + 1) : "" + (fromCal.get(Calendar.MONTH) + 1));
    String date = (fromCal.get(Calendar.DATE) < 10 ? "0" + fromCal.get(Calendar.DATE) : "" + fromCal.get(Calendar.DATE));
    String toMonth = ((toCal.get(Calendar.MONTH) + 1) < 10 ? "0" + (toCal.get(Calendar.MONTH) + 1) : "" + (toCal.get(Calendar.MONTH) + 1));
    String toDate = (toCal.get(Calendar.DATE) < 10 ? "0" + toCal.get(Calendar.DATE) : "" + toCal.get(Calendar.DATE));
    String TimeStamp1 = fromCal.get(Calendar.YEAR) + "-" + month + "-" + date;
    String TimeStamp2 = toCal.get(Calendar.YEAR) + "-" + toMonth + "-" + toDate;
    log.info("Google Analytics Stats for date range " +TimeStamp1 + " to " +TimeStamp2);
    String accessToken = gleTokenInf.getAccessToken();
    String refreshToken = gleTokenInf.getRefreshToken();
    Analytics analytics = null;
    NetHttpTransport netHttpTransport = new NetHttpTransport();

    if (accessToken != null && refreshToken != null) {
        JacksonFactory jacksonFactory = new JacksonFactory();
         GoogleCredential credential =new GoogleCredential.Builder().setTransport(netHttpTransport)
                                               .setJsonFactory(jacksonFactory).setClientSecrets(CLIENT_ID, CLIENT_SECRET).build();
                           credential.setAccessToken(accessToken);
                           credential.setRefreshToken(refreshToken);
        analytics=new Analytics.Builder(netHttpTransport, jacksonFactory, credential).setApplicationName(APPLICATION_NAME).build();
        try {
            //AT a time we can get only 7 dimensions and 10 metrics
            apiQuery = analytics.data().ga().get("ga:" + gleTokenInf.getProfileId(), TimeStamp1, TimeStamp2,getMetrics());
            apiQuery.setDimensions(getDimensions());
            StringBuilder source = new StringBuilder();
            int len = FilterConditions.length;
            for (int i = 0; i < len; i++) {
                   source.append(FilterConditions[i]).append(filterExpression).append(FilterValues[i]).append(FilterType); 
                }
            apiQuery.setFilters(source.substring(0, source.length() - 1));
            gaData = apiQuery.execute();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    return apiQuery;
}  

错误是:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
  "code" : 403,
  "errors" : [ {
    "domain" : "usageLimits",
    "message" : "Daily Limit Exceeded. The quota will be reset at midnight Pacific Time (PT). You may monitor your quota usage and adjust limits in the API Console: https://console.developers.google.com/apis/api/analytics/quotas?project=433676821622",
    "reason" : "dailyLimitExceeded",
    "extendedHelp" : "https://console.developers.google.com/apis/api/analytics/quotas?project=433676821622"
  } ],
  "message" : "Daily Limit Exceeded. The quota will be reset at midnight Pacific Time (PT). You may monitor your quota usage and adjust limits in the API Console
}

----------

metrics: ga:transactions,ga:transactionRevenue,ga:itemRevenue,ga:transactionShipping,ga:transactionTax

dimensions: ga:campaign,ga:adGroup

提前致谢。

1 个答案:

答案 0 :(得分:0)

使用Google Analytics API时,您应注意几个配额。

首先,您的应用程序每天可以发出最多50000个请求。第二个是您尝试访问的每个视图每天最多可以发出10000个请求。

  

超出每日限额。配额将在太平洋时间午夜(PT)重置

表示您已超过其中一个配额。找出它是哪一个的最简单方法是尝试使用不同的视图发出请求。如果它工作,那么你知道你在第一个视图上吹掉了配额。如果它没有意味着你吹掉了项目的配额。除非配额重新启动,否则您将无法再次访问API,直到美国时间西部时间晚上配额重置为止。

您可以请求扩展每个项目的50000个请求。通过Google开发者控制台,您可以在其中启用分析API并在配额部分下。获得延期需要大约一个月的时间,因此当您每天使用80%的配额时,最好申请。

您无法每天请求扩展10000个请求。