从java program open browser获取Google授权,以请求已授予的权限
为了让java程序访问我的google驱动器,我需要使用json凭据文件(请参阅)创建一个oauth2.Credential来获取访问令牌 问题是,当我创建凭证java实例时,java程序打开Internet Explorer并请求驱动器的权限从java program open browser获取Google授权,以请求已授予的权限,java,google-app-engine,batch-file,authorization,Java,Google App Engine,Batch File,Authorization,为了让java程序访问我的google驱动器,我需要使用json凭据文件(请参阅)创建一个oauth2.Credential来获取访问令牌 问题是,当我创建凭证java实例时,java程序打开Internet Explorer并请求驱动器的权限 Credential credential = new AuthorizationCodeInstalledApp(flow , new LocalServerReceiver())
Credential credential = new AuthorizationCodeInstalledApp(flow
, new LocalServerReceiver())
.authorize("user")
;
若我点击“允许”按钮,凭证被创建,我得到一个令牌
System.out.println("token" + credential.getAccessToken());
问题是这个java程序将是一个批处理程序,因此我们不能要求批处理单击按钮
此外,在我的驱动器中已经允许访问我的应用程序(应用程序名为GoogleTest)
您知道如何在没有java程序打开浏览器请求许可的情况下获取凭证吗?
多谢各位
以下是完整的代码:
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();
}
}
公共静态void getToken(){
HttpTransport-HttpTransport;
输入流输入流;
试一试{
httpTransport=GoogleNetHttpTransport.newTrustedTransport();
List scope=Arrays.asList(DriveScopes.DRIVE);
inputStream=新文件inputStream(“C:/dev/ws/mainAzure/GoogleTest/res/client\u secret\u jcb\u inst.json”);
InputStreamReader reader=新的InputStreamReader(inputStream);
clientSecrets=GoogleClientSecrets.load(JSON_工厂,阅读器);
GoogleAuthorizationCodeFlow=新建GoogleAuthorizationCodeFlow.Builder(
高温超导输运
,沙乌工厂
,客户机密
,范围)
.setAccessType(“脱机”)
.setApprovalPrompt(“强制”)
.build();
//浏览器在有新授权代码InstalledApp(…)时打开
凭证凭证=新授权代码已安装App(流程
,新的LocalServerReceiver())
.授权(“用户”)
;
System.out.println(“token”+credential.getAccessToken());
}捕获(例外e){
e、 printStackTrace();
}
}
这是您的授权代码流。您可以重复使用相同的凭证,前提是您访问的是相同的Google帐户
考虑使用刷新令牌手动获取访问代码,并将其手动输入到批处理代码中 经过一些帮助(谢谢哈罗德),我可以找到解决方案:
我们需要定义一个数据存储工厂,在其他地方,refreshToken不会存储在文件中,谷歌每次都会通过互联网浏览器请求许可。
所以我补充说:
dataStoreFactory = new FileDataStoreFactory(new File("C:/dev/ws/mainAzure/GoogleTest/res"));
以及:
成为:
flow = new GoogleAuthorizationCodeFlow.Builder(
httpTransport
, JSON_FACTORY
,clientSecrets
, scope)
.setAccessType("offline")
.setDataStoreFactory(dataStoreFactory)
.setApprovalPrompt("force")
.build();
此外,要获得不限于1小时的访问权限,需要刷新令牌:
credential.refreshToken() ;
谢谢你的回答,但目标是让一个程序从谷歌驱动器访问数据,而不需要手动干预注入访问令牌。我正在寻找一个解决方案。您认为不可能自动获取令牌吗?请查看已安装的GoogleAPI应用程序流。我认为这就是您需要的。在安装程序时,只需执行一次操作,一旦存储了刷新令牌,就不会再次询问,批处理将使用刷新令牌一次又一次地获取新的访问令牌。
credential.refreshToken() ;