Java 缓存Google日历凭据或服务
我有以下代码来使用Java API(使用服务帐户)构建Google日历服务: 它可以在一些基本测试中正常工作,问题是凭证/服务可以重复使用多长时间?i、 e.在重新生成之前,您可以使用它发出多少API请求?此服务器应用程序可能会处理大量API调用,并在重新启动之间持续数月Java 缓存Google日历凭据或服务,java,google-api,google-calendar-api,google-api-java-client,Java,Google Api,Google Calendar Api,Google Api Java Client,我有以下代码来使用Java API(使用服务帐户)构建Google日历服务: 它可以在一些基本测试中正常工作,问题是凭证/服务可以重复使用多长时间?i、 e.在重新生成之前,您可以使用它发出多少API请求?此服务器应用程序可能会处理大量API调用,并在重新启动之间持续数月 做一些计时,凭证构建阶段(GoogleCredential credential=new GoogleCredential.Builder()…)花费的时间最多,约为四分之一秒,我将尝试从缓存开始并查看它的运行情况,但任何答案
做一些计时,凭证构建阶段(GoogleCredential credential=new GoogleCredential.Builder()…)花费的时间最多,约为四分之一秒,我将尝试从缓存开始并查看它的运行情况,但任何答案都值得赞赏。因为您没有直接指定访问令牌,
GoogleCredential
将自动刷新令牌。您似乎已经在跟踪,因此无需更多操作。只要访问令牌有效,服务就可以重复使用,它是短期令牌,因此请求刷新令牌(通过选择offline_type='refresh')检查此链接。每个应用程序对每个API都有一个配额(默认值)。这决定了可以向每个API发出多少请求。检查此链接
/**
* Return a google Calendar object for interacting with the calendar API.
* Return null if it can't be built for any reason
*/
private Calendar buildGoogleCalendarService() throws GeneralSecurityException, IOException {
String googleUsername = this.getGoogleUsername();
if (googleUsername == null) {
return null;
}
String path = AuthManager.class.getClassLoader().getResource("").getPath();
File privateKey = new File(path + "/google_key.p12");
if (!privateKey.exists()) {
logger.error("Google private key not found at " + privateKey.getAbsolutePath());
return null;
}
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport)
.setJsonFactory(jsonFactory).setServiceAccountId(AppProperties.googleAppEmailAddress)
.setServiceAccountPrivateKeyFromP12File(privateKey)
.setServiceAccountScopes(Collections.singleton(CalendarScopes.CALENDAR))
.setServiceAccountUser(googleUsername).build();
Calendar service = new Calendar.Builder(httpTransport, jsonFactory, credential)
.setApplicationName(AppProperties.appName).build();
return service;
}