导入GoogleCredentials以连接到BigQuery

时间:2015-07-24 09:15:59

标签: java oauth-2.0 google-bigquery google-oauth2

在使用Java中的tutorial时,我遇到了连接Google的BigQuery的麻烦。

我的问题是我无法解决方法:

GoogleCredentials.getApplicationDefault(HttpTransport t, JsonFactory f);

即使我正在导入正确的包裹:

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;

我添加了我认为合适的图书馆:

com.google.oauth-client:google-oauth-client:1.19.0

这是我的完整代码:

package com.test.tester;

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.bigquery.Bigquery;
import com.google.api.services.bigquery.BigqueryScopes;

import java.io.IOException;
import java.util.Collection;

public class BigQuerytest {

    private static Bigquery createAuthorizedClient() throws IOException {
        Collection<String> bigqueryScopes = BigqueryScopes.all();
        HttpTransport transport = new NetHttpTransport();
        JsonFactory jsonFactory = new JacksonFactory();
        GoogleCredential credential = GoogleCredential.getApplicationDefault(
                transport, jsonFactory);
        if (credential.createScopedRequired()) {
            credential = credential.createScoped(bigqueryScopes);
        }
        return new Bigquery.Builder(transport, jsonFactory, credential)
                .setApplicationName("BigQuery Samples").build();
    }
}

我相信我的问题来自Google Credentials图书馆,这是不正确的,但我尝试了很多其他图书馆而且都没有...

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

将此添加到您的应用底座文件

compile 'com.google.apis:google-api-services-bigquery:v2-rev245-1.20.0'