Ranjan
Ranjan

Reputation: 868

GoogleCredential.Builder() cannot be resolved

I am developing a Service Account application to obtain Token for accessing downstream services. However, I am facing an issue with compilation. Eclipse prompts an error "The type com.google.api.client.auth.oauth2.Credential$Builder cannot be resolved. It is indirectly referenced from required .class files". Code snippet below:

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("[email protected]")
      .setServiceAccountScopes(BigqueryScopes.BIGQUERY)
      .setServiceAccountPrivateKeyFromP12File(new File("key.p12"))
      .build();
}
}

Please help. Thanks in advance.

Upvotes: 2

Views: 5787

Answers (2)

Samir
Samir

Reputation: 21

I resolved above issue and getting token by adding one more jar in library "google-oauth-client-1.22.0.jar". Below is code snippet

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

Upvotes: 2

Shalaka Deshpande
Shalaka Deshpande

Reputation: 166

I resolved this error with overriding google-api-client dependency to this -

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

Upvotes: 5

Related Questions