GoogleCredential.Builder()无法解析

时间:2014-09-13 16:32:09

标签: oauth-2.0 google-oauth

我正在开发一个服务帐户应用程序来获取用于访问下游服务的令牌。但是,我面临编译问题。 Eclipse提示错误" 无法解析com.google.api.client.auth.oauth2.Credential $ Builder类型。它是从所需的.class文件"间接引用的。以下代码段:

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.jackson.JacksonFactory;


public class AppMain {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    final HttpTransport TRANSPORT = new NetHttpTransport();
    final JsonFactory JSON_FACTORY = new JacksonFactory();

    GoogleCredential credential = new  GoogleCredential.Builder()
      .setTransport(TRANSPORT)
      .setJsonFactory(JSON_FACTORY)
      .setServiceAccountId("SOMETHING@developer.gserviceaccount.com")
      .setServiceAccountScopes(BigqueryScopes.BIGQUERY)
      .setServiceAccountPrivateKeyFromP12File(new File("key.p12"))
      .build();
}
}

请帮忙。提前谢谢。

2 个答案:

答案 0 :(得分:2)

我解决了上述问题,并通过在库“ google-oauth-client-1.22.0.jar”中添加了一个jar来获取令牌。下面是代码段

GoogleCredential googleCredentials = GoogleCredential.fromStream(serviceAccount);
GoogleCredential scoped = googleCredentials.createScoped(
Arrays.asList(
        "https://www.googleapis.com/auth/firebase.messaging"    
            )
    );
scoped.refreshToken();
String token = scoped.getAccessToken();

答案 1 :(得分:1)

我通过重写google-api-client依赖关系来解决此错误 -

    <dependency>
        <groupId>com.google.api-client</groupId>
        <artifactId>google-api-client</artifactId>
        <version>1.22.0</version>
    </dependency>