从java程序打开浏览器获取Google授权,询问已获得许可 [英] Get Google authorization from java program open browser for asking already given permission

查看:877
本文介绍了从java程序打开浏览器获取Google授权,询问已获得许可的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了让一个java程序访问我的谷歌驱动器,我需要使用json凭证文件创建一个oauth2.Credential(请参阅



如果我点击按钮允许,创建Credential并且获得一个令牌。

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





问题是这个java程序将是一个批处理程序所以我们不能要求该批次点击按钮。



另外在



你知道如何在没有Java程序打开浏览器的情况下获得凭证以获取许可?
谢谢

这里是完整的代码:

  public static void getToken(){
HttpTransport httpTransport;
InputStream inputStream;
尝试{
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
列表< 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();

//在新的AuthorizationCodeInstalledApp(...)
凭证凭证=新的AuthorizationCodeInstalledApp(流
,新的LocalServerReceiver())
.authorize( user)
;
System.out.println(token+ credential.getAccessToken());
} catch(Exception e){
e.printStackTrace();


$ / code>


解决方案

After一些帮助(谢谢你哈罗德)我可以找到解决方案:
一个需要定义一个datastorefactory,在其他地方refreshToken没有存储在一个文件中,谷歌通过互联网浏览器每次都要求权限。
所以我补充说:

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



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

成为:

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

此外,如果访问次数不限制为1小时,则需要刷新令牌:

  credential.refreshToken(); 


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

解决方案

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() ;

这篇关于从java程序打开浏览器获取Google授权,询问已获得许可的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆