从 java 程序获得 Google 授权 打开浏览器请求已授予的权限
Get Google authorization from java program open browser for asking already given permission
为了让 java 程序访问我的 google 驱动器,我需要使用 json 凭据文件创建一个 oauth2.Credential(参见 https://console.developers.google.com ) 获取访问令牌。
问题是当我创建凭据 java 实例时 java 程序打开 Internet Explorer 并请求驱动器权限。
Credential credential = new AuthorizationCodeInstalledApp(flow
, new LocalServerReceiver())
.authorize("user")
;
如果我单击按钮 "allow",将创建凭据并获得一个令牌。
System.out.println("token" + credential.getAccessToken());
问题是这个 java 程序将是一个批处理程序,所以我们不能让批处理程序点击一个按钮。
此外 https://security.google.com/settings/security/permissions?pli=1 我的云端硬盘已经允许访问我的应用程序(应用程序名称是 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();
}
}
这就是您的授权代码流程。您可以重复使用相同的凭据,前提是您访问的是相同的 Google 帐户。
考虑使用刷新令牌手动获取访问代码,并将其手动输入到批处理代码中。
经过一些帮助(谢谢哈罗德)我找到了解决方案:
需要定义一个数据存储工厂,其他地方的 refreshToken 不存储在文件中,并且每次通过 Internet 浏览器 Google 请求权限。
所以我添加了:
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
, scope)
.setAccessType("offline")
.setDataStoreFactory(dataStoreFactory)
.setApprovalPrompt("force")
.build();
此外,要获得不限于 1 小时的访问权限,需要刷新令牌:
credential.refreshToken() ;
为了让 java 程序访问我的 google 驱动器,我需要使用 json 凭据文件创建一个 oauth2.Credential(参见 https://console.developers.google.com ) 获取访问令牌。
问题是当我创建凭据 java 实例时 java 程序打开 Internet Explorer 并请求驱动器权限。
Credential credential = new AuthorizationCodeInstalledApp(flow
, new LocalServerReceiver())
.authorize("user")
;
如果我单击按钮 "allow",将创建凭据并获得一个令牌。
System.out.println("token" + credential.getAccessToken());
问题是这个 java 程序将是一个批处理程序,所以我们不能让批处理程序点击一个按钮。
此外 https://security.google.com/settings/security/permissions?pli=1 我的云端硬盘已经允许访问我的应用程序(应用程序名称是 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();
}
}
这就是您的授权代码流程。您可以重复使用相同的凭据,前提是您访问的是相同的 Google 帐户。
考虑使用刷新令牌手动获取访问代码,并将其手动输入到批处理代码中。
经过一些帮助(谢谢哈罗德)我找到了解决方案: 需要定义一个数据存储工厂,其他地方的 refreshToken 不存储在文件中,并且每次通过 Internet 浏览器 Google 请求权限。 所以我添加了:
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
, scope)
.setAccessType("offline")
.setDataStoreFactory(dataStoreFactory)
.setApprovalPrompt("force")
.build();
此外,要获得不限于 1 小时的访问权限,需要刷新令牌:
credential.refreshToken() ;