BigQuery 和 OAuth2 [英] BigQuery and OAuth2

查看:25
本文介绍了BigQuery 和 OAuth2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 服务访问 Google BigQuery帐户方法.我的代码如下:

I'm trying to access Google BigQuery using Service Account approach. My code is as follows:

private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();

private static final JsonFactory JSON_FACTORY = new JacksonFactory();

GoogleCredential credentials = new GoogleCredential.Builder()
            .setTransport(HTTP_TRANSPORT)
            .setJsonFactory(JSON_FACTORY)
            .setServiceAccountId("XXXXX@developer.gserviceaccount.com")
            .setServiceAccountScopes(BigqueryScopes.BIGQUERY)
            .setServiceAccountPrivateKeyFromP12File(
                    new File("PATH-TO-privatekey.p12"))
            .build();
    Bigquery bigquery = Bigquery.builder(HTTP_TRANSPORT, JSON_FACTORY).setHttpRequestInitializer(credentials)
            .build();
    com.google.api.services.bigquery.Bigquery.Datasets.List datasetRequest = bigquery.datasets().list(
            "PROJECT_ID");

    DatasetList datasetList = datasetRequest.execute();
    if (datasetList.getDatasets() != null) {
        java.util.List<Datasets> datasets = datasetList.getDatasets();
        System.out.println("Available datasets
----------------");
        for (Datasets dataset : datasets) {
            System.out.format("%s
", dataset.getDatasetReference().getDatasetId());
        }
    }

但它抛出以下异常:

Exception in thread "main"  com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized
{
  "code" : 401,
  "errors" : [ {
    "domain" : "global",
    "location" : "Authorization",
    "locationType" : "header",
    "message" : "Authorization required",
    "reason" : "required"
  } ],
  "message" : "Authorization required"
}
at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:159)
at com.google.api.client.googleapis.json.GoogleJsonResponseException.execute(GoogleJsonResponseException.java:187)
at com.google.api.client.googleapis.services.GoogleClient.executeUnparsed(GoogleClient.java:115)
at com.google.api.client.http.json.JsonHttpRequest.executeUnparsed(JsonHttpRequest.java:112)
at com.google.api.services.bigquery.Bigquery$Datasets$List.execute(Bigquery.java:979)

在这一行触发异常:

  DatasetList datasetList = datasetRequest.execute();

我从 Google 的 API 控制台获取帐户 ID,该部分的第二行如下所示:

I'm getting the account ID from Google's API console from the second line on the section that looks like this:

    Client ID:  XXXXX.apps.googleusercontent.com
    Email address:  XXXXX@developer.gserviceaccount.com

我错过了什么?

推荐答案

尤里卡!Eric 和 Michael 的代码运行良好.

Eureka! Both Eric's and Michael's code works well.

问题中发布的错误可以通过在客户端机器上错误设置时间来重现.幸运的是,可以通过在客户端正确设置时间来解决.

The error posted in the question can be reproduced by setting the time on the client machine incorrectly. Fortunately, it can be solved by setting the time on the client machine correctly.

注意:就其价值而言,我使用Internet 时间设置"对话框中的立即更新"按钮同步了 Windows 7 框上的时间.我认为这应该是非常防白痴的……但我想我击败了系统.它修正了秒数,但让机器离开了整整一分钟.此后 BigQuery 调用失败.手动改时间成功了.

Note: For what it's worth, I synchronized the time on a Windows 7 box using the "Update now" button in the "Internet Time Settings" dialog. I figured that should be pretty idiot-proof... but I guess I beat the system. It corrected the seconds but left the machine off by exactly one minute. The BigQuery call failed after that. It succeeded after I manually changed the time.

这篇关于BigQuery 和 OAuth2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆