BigQuery和OAuth2 [英] BigQuery and OAuth2

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

问题描述

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

  private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport(); 

private static final JsonFactory JSON_FACTORY = new JacksonFactory();

GoogleCredential凭证=新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(凭证)
.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());
}
}

但它会抛出以下异常:

 线程main中的异常com.google.api.client.googleapis.json.GoogleJsonResponseException:401未授权
{
code:401,
errors:[{
domain:global,
location:Authorization,
locationType:标题,
消息:需要授权,
理由:必需
}],
消息:需要授权
}
at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:159)
at com.google.api.client.googleapis.json.GoogleJsonResponseException.execute(GoogleJsonResponseException .java:187)
,位于com.google.api.client.googleapis.services.GoogleClient.executeUnparsed(GoogleClient.java:115)
位于com.google.api.client.http.json.JsonHttpRequest .executeUnparsed(JsonHttpRequest.java:112)
,位于com.google.api.servic es.bigquery.Bigquery $ Datasets $ List.execute(Bigquery.java:979)

例外是
$ b

  DatasetList datasetList = datasetRequest.execute(); 

我从第二行的Google API控制台获取帐户ID这:

 客户端ID:XXXXX.apps.googleusercontent.com 
电子邮件地址:XXXXX@developer.gserviceaccount.com

我缺少什么?

解决方案

尤里卡! Eric和Michael的代码都能正常工作。

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



注意:对于它的价值,我将时间同步在Internet时间设置对话框中使用立即更新按钮的Windows 7框。我想这应该是非常愚蠢的证明......但我想我击败了系统。它校正了秒数,但将机器关闭了一分钟。之后,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\n----------------");
        for (Datasets dataset : datasets) {
            System.out.format("%s\n", dataset.getDatasetReference().getDatasetId());
        }
    }

But it throws the following exception:

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)

The exception is fired on this line:

  DatasetList datasetList = datasetRequest.execute();

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

What am I missing?

解决方案

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.

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天全站免登陆