package org.dataportabilityproject.serviceProviders.google;

import com.google.api.client.auth.oauth2.AuthorizationCodeFlow;
import com.google.api.client.auth.oauth2.BearerToken;
import com.google.api.client.auth.oauth2.ClientParametersAuthentication;
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.http.GenericUrl;

import java.util.List;

import static com.google.api.client.googleapis.auth.oauth2.GoogleOAuthConstants.AUTHORIZATION_SERVER_URL;
import static com.google.api.client.googleapis.auth.oauth2.GoogleOAuthConstants.TOKEN_SERVER_URL;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.dataportabilityproject.serviceProviders.google.GoogleStaticObjects.JSON_FACTORY;

/**
 * A generator of Google {@link Credential}
 */
public class CredentialGenerator {
    /** Port in the "Callback URL". */
    public static final int PORT = 8080;

    /** Domain name in the "Callback URL". */
    public static final String DOMAIN = "127.0.0.1";

    private final String clientId;
    private final String apiSecret;

    public CredentialGenerator(String clientId, String apiSecret) {
        this.clientId = checkNotNull(clientId);
        this.apiSecret = checkNotNull(apiSecret);
    }

    public Credential authorize(List<String> scopes) throws Exception {
        // set up authorization code flow
        AuthorizationCodeFlow flow = new AuthorizationCodeFlow.Builder(BearerToken
                .authorizationHeaderAccessMethod(),
                GoogleStaticObjects.getHttpTransport(),
                JSON_FACTORY,
                new GenericUrl(TOKEN_SERVER_URL),
                new ClientParametersAuthentication(clientId, apiSecret),
                clientId,
                AUTHORIZATION_SERVER_URL).setScopes(scopes)
                .setDataStoreFactory(GoogleStaticObjects.getDataStoreFactory()).build();
        // authorize
        LocalServerReceiver receiver = new LocalServerReceiver.Builder().setHost(DOMAIN).setPort(PORT).build();
        return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
    }
}

@@ -53,32 +53,39 @@
 google;%0A%0Aimport 
+static 
 com.google.api.c
@@ -83,180 +83,147 @@
 gle.
-api.client.auth.oauth2.AuthorizationCodeFlow;%0Aimport com.google.api.client.auth.oauth2.BearerToken;%0Aimport com.google.api.client.auth.oauth2.ClientParametersAuthentication;
+common.base.Preconditions.checkNotNull;%0Aimport static org.dataportabilityproject.serviceProviders.google.GoogleStaticObjects.JSON_FACTORY;%0A
 %0Aimp
@@ -451,86 +451,8 @@
 ort 
-com.google.api.client.http.GenericUrl;%0A%0Aimport java.util.List;%0A%0Aimport static 
 com.
@@ -502,309 +502,52 @@
 ogle
-O
 Auth
-Constants.AUTHORIZATION_SERVER_URL;%0Aimport static com.google.api.client.googleapis.auth.oauth2.GoogleOAuthConstants.TOKEN_SERVER_URL;%0Aimport static com.google.common.base.Preconditions.checkNotNull;%0Aimport static org.dataportabilityproject.serviceProviders.google.GoogleStaticObjects.JSON_FACTORY
+orizationCodeFlow;%0Aimport java.util.List
 ;%0A%0A/
@@ -1178,18 +1178,22 @@
 w%0A      
-  
+Google
 Authoriz
@@ -1213,24 +1213,30 @@
  flow = new 
+Google
 Authorizatio
@@ -1257,369 +1257,142 @@
 der(
-BearerToken%0A                .authorizationHeaderAccessMethod(),%0A                GoogleStaticObjects.getHttpTransport(),%0A                JSON_FACTORY,%0A                new GenericUrl(TOKEN_SERVER_URL),%0A                new ClientParametersAuthentication(clientId, apiSecret),%0A                clientId,%0A                AUTHORIZATION_SERVER_URL).setScopes(scopes)%0A  
+%0A          GoogleStaticObjects.getHttpTransport(), JSON_FACTORY, clientId, apiSecret, scopes)%0A              .setAccessType(%22offline%22)%0A
     
@@ -1464,16 +1464,31 @@
 ctory())
+%0A              
 .build()
@@ -1637,16 +1637,17 @@
  return 
+ 
 new Auth

Use GoogleAuthorizationCodeFlow with offline access type