如何延长 Oauth 2 StoredCredential 的持续时间
How to extend the duration of a Oauth 2 StoredCredential
我正在制作一个 API 来获取 google sheet 的内容,但是每隔一小时,令牌“StoredCredential”就会过期,所以我必须使用 UI(您选择 google 帐户的菜单),我如何才能延长令牌的使用寿命,因为该程序每天都会启动,我不想每次都输入我的 google 帐户天?
我已经尝试更改过期时间,但没有用
这是我的实际代码:
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static final String TOKENS_DIRECTORY_PATH = "u:\tokens";
private static final List<String> SCOPES =
Arrays.asList(SheetsScopes.SPREADSHEETS,SheetsScopes.DRIVE);
private static final String CREDENTIALS_FILE_PATH = "credentials.json";
private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws Exception {
// Load client secrets.
InputStream in = new FileInputStream(CREDENTIALS_FILE_PATH);
if (in == null) {
throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
}
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
FileDataStoreFactory fileDataStoreFactory = new FileDataStoreFactory(new File(TOKENS_DIRECTORY_PATH));
DataStore<StoredCredential> datastore = fileDataStoreFactory.getDataStore("StoredCredential");
datastore.values().forEach(x -> x.setExpirationTimeMilliseconds((long) 999999999));
//DEBUG
datastore.values().forEach(x -> System.out.println(x.getExpirationTimeMilliseconds()));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
//.setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
.setCredentialDataStore(datastore)
.setAccessType("online")
.setApprovalPrompt(null)
.build();
LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
}
阅读我的其他问题的文档后,我找到了延长 StoredCredential 文件生命周期的方法,您需要将访问类型从在线更改为离线。
更正版本:
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static final String TOKENS_DIRECTORY_PATH = "u:\tokens";
private static final List<String> SCOPES =
Arrays.asList(SheetsScopes.SPREADSHEETS,SheetsScopes.DRIVE);
private static final String CREDENTIALS_FILE_PATH = "credentials.json";
private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws Exception {
// Load client secrets.
InputStream in = new FileInputStream(CREDENTIALS_FILE_PATH);
if (in == null) {
throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
}
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
FileDataStoreFactory fileDataStoreFactory = new FileDataStoreFactory(new File(TOKENS_DIRECTORY_PATH));
DataStore<StoredCredential> datastore = fileDataStoreFactory.getDataStore("StoredCredential");
datastore.values().forEach(x -> x.setExpirationTimeMilliseconds((long) 999999999));
//DEBUG
datastore.values().forEach(x -> System.out.println(x.getExpirationTimeMilliseconds()));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
//.setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
.setCredentialDataStore(datastore)
/* Here, we change the accessType */
.setAccessType("offline")
.setApprovalPrompt(null)
.build();
LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
我正在制作一个 API 来获取 google sheet 的内容,但是每隔一小时,令牌“StoredCredential”就会过期,所以我必须使用 UI(您选择 google 帐户的菜单),我如何才能延长令牌的使用寿命,因为该程序每天都会启动,我不想每次都输入我的 google 帐户天?
我已经尝试更改过期时间,但没有用
这是我的实际代码:
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static final String TOKENS_DIRECTORY_PATH = "u:\tokens";
private static final List<String> SCOPES =
Arrays.asList(SheetsScopes.SPREADSHEETS,SheetsScopes.DRIVE);
private static final String CREDENTIALS_FILE_PATH = "credentials.json";
private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws Exception {
// Load client secrets.
InputStream in = new FileInputStream(CREDENTIALS_FILE_PATH);
if (in == null) {
throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
}
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
FileDataStoreFactory fileDataStoreFactory = new FileDataStoreFactory(new File(TOKENS_DIRECTORY_PATH));
DataStore<StoredCredential> datastore = fileDataStoreFactory.getDataStore("StoredCredential");
datastore.values().forEach(x -> x.setExpirationTimeMilliseconds((long) 999999999));
//DEBUG
datastore.values().forEach(x -> System.out.println(x.getExpirationTimeMilliseconds()));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
//.setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
.setCredentialDataStore(datastore)
.setAccessType("online")
.setApprovalPrompt(null)
.build();
LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
}
阅读我的其他问题的文档后,我找到了延长 StoredCredential 文件生命周期的方法,您需要将访问类型从在线更改为离线。
更正版本:
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static final String TOKENS_DIRECTORY_PATH = "u:\tokens";
private static final List<String> SCOPES =
Arrays.asList(SheetsScopes.SPREADSHEETS,SheetsScopes.DRIVE);
private static final String CREDENTIALS_FILE_PATH = "credentials.json";
private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws Exception {
// Load client secrets.
InputStream in = new FileInputStream(CREDENTIALS_FILE_PATH);
if (in == null) {
throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
}
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
FileDataStoreFactory fileDataStoreFactory = new FileDataStoreFactory(new File(TOKENS_DIRECTORY_PATH));
DataStore<StoredCredential> datastore = fileDataStoreFactory.getDataStore("StoredCredential");
datastore.values().forEach(x -> x.setExpirationTimeMilliseconds((long) 999999999));
//DEBUG
datastore.values().forEach(x -> System.out.println(x.getExpirationTimeMilliseconds()));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
//.setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
.setCredentialDataStore(datastore)
/* Here, we change the accessType */
.setAccessType("offline")
.setApprovalPrompt(null)
.build();
LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();