如何在java中使用googledrive服务器密钥
我有一个从客户端连接到google drive的代码片段,它可以在带有浏览器的桌面上正常工作如何在java中使用googledrive服务器密钥,java,google-drive-api,google-docs-api,Java,Google Drive Api,Google Docs Api,我有一个从客户端连接到google drive的代码片段,它可以在带有浏览器的桌面上正常工作 public static Credential authorize() throws IOException { // Load client secrets. InputStream in = DriveQuickstart.class.getResourceAsStream("/client_secret.json"); GoogleClientSe
public static Credential authorize() throws IOException {
// Load client secrets.
InputStream in = DriveQuickstart.class.getResourceAsStream("/client_secret.json");
GoogleClientSecrets clientSecrets
= GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow
= new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(DATA_STORE_FACTORY)
.setAccessType("offline")
.build();
Credential credential = new AuthorizationCodeInstalledApp(
flow, new LocalServerReceiver()).authorize("user");
System.out.println(
"Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
return credential;
}
/**
* Build and return an authorized Drive client service.
*
* @return an authorized Drive client service
* @throws IOException
*/
public static Drive getDriveService() throws IOException {
Credential credential = authorize();
return new Drive.Builder(
HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME)
.build();
}
此代码打开默认浏览器并提示用户
允许或拒绝
访问驱动器并将凭据存储在本地存储库中
我无法在Linux机器上运行此功能(没有浏览器,wget也会出现故障)
我应该使用OAuth还是使用API密钥(服务器密钥)或任何其他方法来解决此问题。在Linux中,您使用的浏览器可能存在代理问题,需要在不使用代理的情况下进行检查您是否基于API的驱动器来实现此功能?如果您想执行web授权,建议您使用我有一个在Rhel上生成的csv文件(oracle db生成该文件)。我正在尝试将该文件上载到google drive。简单的独立java实用程序来上传文件。我不确定这是否有帮助,但你可以尝试一下,这将像一个中间件。由于Linux机器是无头的,您的web服务器将使用与服务帐户交互的.csv文件-希望这会将文件上载到驱动器。