如何在java中使用Google Drive服务器密钥

时间:2016-01-09 14:10:00

标签: java google-drive-api google-docs-api

我有一个代码片段可以从客户端连接到谷歌驱动器,它可以在带有浏览器的桌面上正常工作。

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​​密钥(服务器密钥)或任何其他方法来解决此问题。

0 个答案:

没有答案