Google API重定向更改不适用

时间:2017-10-06 07:58:15

标签: java google-api google-drive-api google-oauth google-api-java-client

我一直在开发一个使用Google Drive API的应用,因此我使用OAuth ID客户端重定向到http://localhost:8080/Callback,但现在我已将其发布,因此我将其更改为http://myURL/Callback

但是,更改似乎不适用,因为它要求打开以下网址:

https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=my_client_id&redirect_uri=http://localhost:46517/Callback&response_type=code&scope=https://www.googleapis.com/auth/drive

如果我尝试打开它,我显然得到Error: redirect_uri_mismatch,因为我删除了localhost作为授权重定向。

以下是我用来连接Google API的代码

public FileSP downloadFile(String fileId, String clientSecretPath){

    FileSP fileSP = null;
    String extension = null;
    try {

        Drive driveService = _getDriveService(clientSecretPath);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        File file = driveService.files().get(fileId).execute();
       //do stuff

    } catch (IOException e) {
        String mess = "downloadFile(): " 
                + (e.getMessage()!=null?". "+e.getMessage():"")
                + (e.getCause()!=null?". "+e.getCause():"");
        logger.error(mess);
    }
    return fileSP;
}


private Drive _getDriveService(String clientSecretPath) throws IOException {
    Credential credential = _authorize(clientSecretPath);
    return new Drive.Builder(
            HTTP_TRANSPORT, JSON_FACTORY, credential)
            .setApplicationName(APPLICATION_NAME)
            .build();
}


private Credential _authorize(String clientSecretPath) throws IOException {

    GoogleClientSecrets clientSecrets =
        GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(new FileInputStream(clientSecretPath)));

    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;
}

0 个答案:

没有答案