java获取authorization_从java程序打开浏览器获取Google授权,询问已获得许可

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值