Heroku google drive Oauth 2.0 for java web应用程序
我的项目是我在heroku上主持的java spark项目。该项目的一部分要求我将一个特定的文件从我的谷歌硬盘下载到应用程序中。当我的应用程序在本地运行时,我设法设置了一切,但一旦我部署了应用程序,它就停止工作了。这与以下事实无关:当应用程序在本地运行时,我使用了类型为other的Oauth2客户端ID,当我部署应用程序时,我创建了一个类型为web application的新应用程序 以下是身份验证和下载代码的片段:Heroku google drive Oauth 2.0 for java web应用程序,heroku,google-drive-api,google-oauth,google-oauth-java-client,Heroku,Google Drive Api,Google Oauth,Google Oauth Java Client,我的项目是我在heroku上主持的java spark项目。该项目的一部分要求我将一个特定的文件从我的谷歌硬盘下载到应用程序中。当我的应用程序在本地运行时,我设法设置了一切,但一旦我部署了应用程序,它就停止工作了。这与以下事实无关:当应用程序在本地运行时,我使用了类型为other的Oauth2客户端ID,当我部署应用程序时,我创建了一个类型为web application的新应用程序 以下是身份验证和下载代码的片段: public class AutoCalls { /** Appli
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();
}
}
尝试在浏览器中打开URL时,出现错误:
Error: redirect_uri_mismatch
我找不到关于在java web应用程序上访问google drive内容的任何好的分步教程,因此任何帮助都将被取消。通常,如果OAuth在服务器a上工作,而不是在服务器B上,那是因为API控制台上没有正确配置重定向URL。没有理由需要使用不同的客户端ID,因为单个客户端ID可以配置多个URL,例如本地主机和heroku服务器。另一种可能性是使用https
代码中有一个单独的问题,您在
lista.isEmpty()
上进行迭代。您应该在nextPageToken!=空
。请参见的答案。我在本地运行应用程序,然后将其移动到Heroku,这一事实是否没有任何意义?我这样问是因为客户端ID可以用于web应用程序和其他(例如,对于没有重定向URL的桌面应用程序)等。正在工作的客户端ID是其他类型的,因此不接受重定向URL。我不明白您为什么更改客户端类型。当然,您的应用程序在测试中应该与在生产中的应用程序相同。因此,标准方法只是为测试环境和生产环境提供一个带有重定向URL的单一客户机ID。要么我误解了您试图实现的目标,要么您误解了OAuth/客户端类型。我更改了我的客户端类型,因为我使用的客户端类型(其他)不允许重定向URL,因此不起作用。听起来您好像还不太了解OAuth和验证流。我建议退一步,读点书。
Error: redirect_uri_mismatch