package data;

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.store.MemoryDataStoreFactory;
import com.google.api.services.sheets.v4.SheetsScopes;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.GeneralSecurityException;
import java.util.Arrays;
import java.util.List;

/** 
 * A class to create and send a Credential object that allows for Sheets API interaction 
 */
public class SheetsAuthUtil {
    public static Credential authorize() throws IOException, GeneralSecurityException {
         
        // Build GoogleClientSecrets from JSON file.
        InputStream in = SheetsAuthUtil.class.getResourceAsStream("/google-sheets-api.json");
        GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JacksonFactory.getDefaultInstance(), new InputStreamReader(in));

        List<String> scopes = Arrays.asList(SheetsScopes.SPREADSHEETS);
 
        // Build Credential object.
        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
            GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), 
            clientSecrets, scopes).setDataStoreFactory(new MemoryDataStoreFactory()).
            setAccessType("offline").build();
        Credential credential = new AuthorizationCodeInstalledApp(
            flow, new LocalServerReceiver()).authorize("user");

        return credential;
    }
}


package data;

import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.sheets.v4.SheetsScopes;
import com.google.api.client.googleapis.extensions.appengine.auth.oauth2.AppIdentityCredential;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.GeneralSecurityException;
import java.util.Arrays;
import java.util.List;

/** 
 * A class to create and send an AppIdentityCredential object that allows for Sheets API interaction 
 */
public class SheetsAuthUtil {
  public static AppIdentityCredential authorize() throws IOException, GeneralSecurityException {
         
    // Build GoogleClientSecrets from JSON file.
    InputStream in = SheetsAuthUtil.class.getResourceAsStream("/google-sheets-api.json");
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JacksonFactory.getDefaultInstance(), new InputStreamReader(in));

    List<String> scopes = Arrays.asList(SheetsScopes.SPREADSHEETS);
 
    AppIdentityCredential credential = new AppIdentityCredential(scopes);

    return credential;
  }
}


switch from using a Credential to an AppIdentityCredential object