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;
    }
}


@@ -41,604 +41,261 @@
 ent.
-auth.oauth2.Credential;%0Aimport com.
 google
-.
 api
-.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;%0Aimport com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;%0Aimport com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;%0Aimport com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;%0Aimport com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;%0Aimport com.google.api.client.json.jackson2.JacksonFactory;%0Aimport com.google.api.client.util.store.MemoryDataStoreFactory;%0Aimport com.google.api.services.sheets.v4.SheetsScopes;%0A
+s.auth.oauth2.GoogleClientSecrets;%0Aimport com.google.api.client.json.jackson2.JacksonFactory;%0Aimport com.google.api.services.sheets.v4.SheetsScopes;%0Aimport com.google.api.client.googleapis.extensions.appengine.auth.oauth2.AppIdentityCredential;
 %0Aimp
@@ -513,17 +513,29 @@
 d send a
- 
+n AppIdentity
 Credenti
@@ -618,18 +618,16 @@
 hUtil %7B%0A
-  
   public
@@ -634,16 +634,27 @@
  static 
+AppIdentity
 Credenti
@@ -725,20 +725,16 @@
        %0A
-    
     // B
@@ -778,20 +778,16 @@
 le.%0A    
-  
-  
 InputStr
@@ -864,20 +864,16 @@
 json%22);%0A
-    
     Goog
@@ -999,20 +999,16 @@
 );%0A%0A    
- 
-   
 List%3CStr
@@ -1073,488 +1073,79 @@
     
-    // Build Credential object.%0A        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(%0A            GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), %0A            clientSecrets, scopes).setDataStoreFactory(new MemoryDataStoreFactory()).%0A            setAccessType(%22offline%22).build();%0A        Credential credential = new AuthorizationCodeInstalledApp(%0A            flow, new LocalServerReceiver()).authorize(%22user%22);%0A%0A    
+AppIdentityCredential credential = new AppIdentityCredential(scopes);%0A%0A
     
@@ -1163,17 +1163,15 @@
 ential;%0A
-  
   %7D%0A%7D%0A%0A

switch from using a Credential to an AppIdentityCredential object