0

我的项目是我在 heroku 上托管的 java spark 项目。项目的一部分要求我从我的谷歌驱动器下载一个特定的文件到应用程序中。当我的应用程序在本地运行时,我设法设置了所有内容,但是一旦我部署了应用程序,它就停止了工作。这与以下事实无关,即当应用程序在本地运行时,我使用了 other 类型的 Oauth2 客户端 ID,而当我部署应用程序时,我创建了一个新的 Web 应用程序类型。

以下是验证和下载代码的片段:

public class AutoCalls {

    /** Application name. */
    private static final String APPLICATION_NAME = "calls-made";
    private static final java.io.File DATA_STORE_DIR = new java.io.File(System.getProperty("target/classes/auth"), ".credentials/calls-made");
    private static FileDataStoreFactory DATA_STORE_FACTORY;
    private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
    private static HttpTransport HTTP_TRANSPORT;
    private static final List<String> SCOPES = Arrays.asList(DriveScopes.DRIVE);

    static {
        try {
            HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
            DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);

        } catch (GeneralSecurityException | IOException t) {
            System.exit(1);
        }
    }

    public static void startDownload() throws IOException, ParseException {

        Drive serv = getDriveService();
     // drive stuff deleted
    }

    public static Credential authorize() throws IOException {
        InputStream in = new FileInputStream("target/classes/auth/client_secret_*****************************************.apps.googleusercontent.com.json");
        GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
        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;
    }

    public static Drive getDriveService() throws IOException {
        Credential credential = authorize();

        return new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
            .setApplicationName(APPLICATION_NAME)
            .build();
    }
}

目前,当我尝试启动下载时,我在 Heroku 日志中得到一个 URL,如下所示:

2017-02-20T10:32:28.656820+00:00 app[web.1]: Please open the following address in your browser:
2017-02-20T10:32:28.656908+00:00 app[web.1]:   https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=************-vvdi7u6bmp1vc90sdidtnuiftdi1t49c.apps.googleusercontent.com&redirect_uri=http://localhost:48085/Callback&response_type=code&scope=https://www.googleapis.com/auth/drive

当我尝试在浏览器中打开 URL 时,出现错误:

Error: redirect_uri_mismatch

我找不到任何关于在 Java Web 应用程序上访问谷歌驱动器内容的好的分步教程,所以任何帮助都会得到帮助。

4

1 回答 1

0

通常,如果 OAuth 在服务器 A 上有效,但在服务器 B 上无效,这是因为在 API 控制台上未正确配置重定向 URL。没有理由需要使用不同的客户端 ID,因为单个客户端 ID 可以配置多个 URL,例如您的本地主机和 Heroku 服务器。另一种可能性是使用 https。

在您迭代的代码中存在一个单独的问题lista.isEmpty()。你应该迭代nextPageToken != null. 请参阅 Google drive rest API 的答案,仅从根文件夹下载文件

于 2017-02-20T13:48:13.693 回答