Get Google authorization from java program open browser for asking already given permission

狂风中的少年 提交于 2019-12-24 01:59:06

问题


In order for a java program to access my google drive I need to create an oauth2.Credential using a json credential file (see https://console.developers.google.com) for getting an access token.

The problem is when I create the Credential java instance the java program open Internet Explorer and ask permission for Drive.

Credential credential = new AuthorizationCodeInstalledApp(flow
                 , new LocalServerReceiver())
                 .authorize("user")
                 ;

If I click button "allow" , Credential is created and I get a token.

 System.out.println("token" + credential.getAccessToken());

The problem is that this java program will be a batch program so we can't ask the batch to click on a button.

Furthermore in https://security.google.com/settings/security/permissions?pli=1 my Drive has already given access to my application (application name is GoogleTest) ...

Do you know how to get the credential without java program open browser for asking permission ? Thank you

Here the full code :

 public static void getToken ()  {
     HttpTransport httpTransport ;
     InputStream inputStream;       
     try {
         httpTransport = GoogleNetHttpTransport.newTrustedTransport();
         List<String> scope = Arrays.asList(DriveScopes.DRIVE);
         inputStream = new FileInputStream("C:/dev/ws/mainAzure/GoogleTest/res/client_secret_jcb_inst.json");
         InputStreamReader reader = new InputStreamReader(inputStream);
         clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,reader);                             
         GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
            httpTransport
             , JSON_FACTORY
             ,clientSecrets
             , scope)
             .setAccessType("offline")
             .setApprovalPrompt("force")
             .build();                  

         //Browser open when there is new AuthorizationCodeInstalledApp(...)
         Credential credential =    new AuthorizationCodeInstalledApp(flow
             , new LocalServerReceiver())
             .authorize("user")
             ;
     System.out.println("token" + credential.getAccessToken());
     } catch (Exception e) {
         e.printStackTrace();
     }
 }

回答1:


After some help (thank you Harold) I could find the solution: One need to define a datastorefactory, elsewhere the refreshToken is not stored in a file and Google via Internet browser ask for permission each time. So I added :

dataStoreFactory = new FileDataStoreFactory(new File("C:/dev/ws/mainAzure/GoogleTest/res"));

and :

 GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
            httpTransport
             , JSON_FACTORY
             ,clientSecrets
             , scope)
             .setAccessType("offline")
             .setApprovalPrompt("force")
             .build(); 

become :

            flow = new GoogleAuthorizationCodeFlow.Builder(
                httpTransport
                 , JSON_FACTORY
                 ,clientSecrets
                 , scope)                     
                 .setAccessType("offline")
                 .setDataStoreFactory(dataStoreFactory)
                 .setApprovalPrompt("force")                 
                 .build();

Furthermore to have an access non limited to 1 hour, one need to refresh the token :

credential.refreshToken() ;



回答2:


That's authorization code flow for you. You can re-use the same credential, provided you are accessing the same Google account.

Consider getting the access code manually with refresh tokens, and manually inputing it into the batch code.



来源:https://stackoverflow.com/questions/38722083/get-google-authorization-from-java-program-open-browser-for-asking-already-given

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!