Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8339039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:46:26+00:00 2026-06-09T04:46:26+00:00

I am attempting to use oauth API to authenticate a google service account through

  • 0

I am attempting to use oauth API to authenticate a google service account through the Java API. I am hoping to use it to access Google Bigquery. I get an “invalid grant” returned from my API requests.

Here is the code, which is a copy of a basic authentication example (which wasn’t for Bigquery.. but another Google API):

  /** Global instance of the HTTP transport. */
  private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();

  /** Global instance of the JSON factory. */
  private static final JsonFactory JSON_FACTORY = new JacksonFactory();

  private static Bigquery bigquery;  

public ServiceAccountExample() {

      try {
          try {

            GoogleCredential credential = new  GoogleCredential.Builder().setTransport(HTTP_TRANSPORT)
                .setJsonFactory(JSON_FACTORY)
                .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
                .setServiceAccountScopes(BigqueryScopes.BIGQUERY)
                .setServiceAccountPrivateKeyFromP12File(new File("GoogleBigQuery-privatekey.p12"))
                //.setRefreshListeners(refreshListeners)
                //.setServiceAccountUser("email.com")
                .build();


            credential.refreshToken();

            bigquery = new Bigquery.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
                        //.setApplicationName("GoogleBigQuery/1.0")
                        .build();

            listDatasets(bigquery, "publicdata");

            return;
          } catch (IOException e) {
            System.err.println(e.getMessage());
          }
        } catch (Throwable t) {
          t.printStackTrace();
        }

}

SERVICE_ACCOUNT_EMAIL is the email address of the form: XXXXXXX@developer.gserviceaccount.com

If I remove the credential.refreshToken() line, it fails in the listsDatasets on the first call to Bigquery… Otherwise it fails on credential.refreshToken().. with the same error.

Does BigQuery not accept Service Account authentication?

I believe I have done everything correctly through the API Console. I have:

  • Turned access to the Big Query API on.
  • Created a service account under the “API Access” tab.
  • Downloaded my private key (which is referenced from the code above).
  • Given my service account user “can edit” access under the “Teams” tab.
  • Enabled Billing.

Have I missed anything? Is there anything else I need to do?

Thanks..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T04:46:27+00:00Added an answer on June 9, 2026 at 4:46 am

    The service account authorization method works just fine with BigQuery. You don’t need to call credential.refreshToken(). Make sure that your private key file can be read from your application. Here is an example:

    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;
    
    import com.google.api.services.bigquery.Bigquery;
    import com.google.api.services.bigquery.BigqueryScopes;
    import com.google.api.services.bigquery.Bigquery.Projects;
    import com.google.api.services.bigquery.model.*;
    
    import java.io.File;
    import java.io.IOException;
    import java.security.GeneralSecurityException;
    import java.util.List;
    
    public class BigQueryJavaServiceAccount {
    
      public static void main(String[] args) throws IOException, InterruptedException, GeneralSecurityException {
    
        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();
    
        Bigquery bigquery = Bigquery.builder(TRANSPORT, JSON_FACTORY)
          .setHttpRequestInitializer(credential)
          .build();
    
        Projects.List projectListRequest = bigquery.projects().list();
        ProjectList projectList = projectListRequest.execute();
        List<ProjectList.Projects> projects = projectList.getProjects();
        System.out.println("Available projects\n----------------\n");
        for (ProjectList.Projects project : projects) {
          System.out.format("%s\n", project.getProjectReference().getProjectId());
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been attempting to use ASP.NET MVC remote validation for username login access
Attempting to use the amazon API to obtain product data and currently failing miserably.
I am attempting to use an Intent Service to run in the background to
I am trying to use the gtm-oauth kit (http://code.google.com/p/gtm-oauth/wiki/GTMOAuthIntroduction) for OAuth2. As instructed by
I'm attempting to use Google's Guava ImmutableSet class to create a set of immutable
I'm attempting to use the Github v3 API and posting JSON to update a
I'm attempting to use the Java Selenium client with JBehave to interact with a
I'm attempting to use Assembly.GetType(MyCompany.Class1.Class2) to dynamically get a type from a string. Assembly.GetType(MyCompany.Class1);
I am attempting use Java Pathfinder and I have pathfinder working. import gov.nasa.jpf.jvm.Verify; user.java:2:
When attempting to use the TCPProxy tool included with The Grinder, I get the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.