Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
How do I open GoogleSheets Service with a Stored Refresh Token using Java API v4

I am working on an application (Apache Drill) and looking to build a connector to Google Sheets using the Google's Java API (v4).

I have this working, however, I would like to store the refresh token and here's where I'm stuck. Here's my existing code:

  public static Credential authorize(GoogleSheetsStoragePluginConfig config) throws IOException, GeneralSecurityException {
    GoogleClientSecrets clientSecrets = config.getSecrets();
    GoogleAuthorizationCodeFlow flow;
    List<String> scopes = Collections.singletonList(SheetsScopes.SPREADSHEETS);

    flow = new GoogleAuthorizationCodeFlow.Builder
      (GoogleNetHttpTransport.newTrustedTransport(), JSON_FACTORY, clientSecrets, scopes)
        .setDataStoreFactory(config.getDataStoreFactory())
        .setAccessType("offline")
        .build();
    return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
  }

  public static Sheets getSheetsService(GoogleSheetsStoragePluginConfig config) throws IOException, GeneralSecurityException {

    Credential credential = GoogleSheetsUtils.authorize(config);
    return new Sheets.Builder(
      GoogleNetHttpTransport.newTrustedTransport(), GsonFactory.getDefaultInstance(), credential)
      .setApplicationName("Drill")
      .build();
  }

What I'd like to have is something like this:

public static Sheets getSheetsService(<client secrets>, String refreshToken, String accessToken) {
   // Not sure what to do here...
}

Any help would be greatly appreciated. 
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The Google api java client library is designed to handle all this for you. By default FileDataStoreFactory stores all of the credetinals with in a file on your machine in DATA_STORE_DIR.

dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);

If you dont want to store it in a file then you just need to create your own implementation of AbstractDataStoreFactory which will accept your refresh token.

Full FileDataStoreFactory sample

/**
   * Initializes an authorized sheets service object.
   *
   * @return The sheets service object.
   * @throws IOException
   * @throws GeneralSecurityException
   */
  private static Sheets initializeAnalyticsReporting() throws GeneralSecurityException, IOException {

    httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);

    // Load client secrets.
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
        new InputStreamReader(HelloSheets.class
            .getResourceAsStream(CLIENT_SECRET_JSON_RESOURCE)));

    // Set up authorization code flow for all authorization scopes.
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow
        .Builder(httpTransport, JSON_FACTORY, clientSecrets,
            SheetsScopes.all()).setDataStoreFactory(dataStoreFactory)
        .build();

    // Authorize.
    Credential credential = new AuthorizationCodeInstalledApp(flow,
        new LocalServerReceiver()).authorize("user");
    // Construct the sheets service object.
    return new Sheets.Builder(httpTransport, JSON_FACTORY, credential)
        .setApplicationName(APPLICATION_NAME).build();
  }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!