java - Java : Google AppEngine : OAuth : Google Drive API 用于访问由 Web 应用程序创建的用户的 Google Drive 内容
问题描述
场景:我正在 Google AppEngine 上开发一个 Web 应用程序,用户必须从他们自己的 Google Drive 访问文件。我一直在寻找在线帮助,这就是我想出的。
我已使用此链接寻求帮助,该链接在使用本地机器进行测试时运行良好 https://github.com/gsuitedevs/java-samples/blob/master/drive/quickstart/src/main/java/DriveQuickstart.java
第 1 步(看似简单):启用 Google Drive API 并设置必要的凭据,即 Web 应用程序的 OAuth 2.0 客户端 ID。(这是在 Google AppEngine 上启用 IAP 时完成的,所以我没有再这样做。每当有人打开 Web 应用程序时,都会要求他/她通过 iap.googleapis.com 进行身份验证并保存详细信息。这很好用)。此外,我已将 Google Drive Scopes 添加到不需要 Google 验证的 OAuth 同意屏幕 (../auth/drive.appdata & ../auth/drive.file)。
第 2 步:从 OAuth 客户端 ID 下载 credentials.json 并存储在应用程序包根目录中创建的“resources”文件夹中(在主文件夹中,在 java 和 webapp 文件夹旁边)
第 3 步:我创建了一个包含以下代码的测试类 (GoogleDriveServiceTest.class):
String USER_ID1 = UserServiceFactory.getUserService().getCurrentUser().getUserId();
List<String> SCOPES = Collections.singletonList(DriveScopes.DRIVE_METADATA_READONLY);
NetHttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
InputStream inputStream =
GoogleDriveServiceTest.class.getResourceAsStream("/credentials.json");
if (inputStream == null)
throw new FileNotFoundException("Required credentials file not found");
GoogleClientSecrets googleClientSecrets =
GoogleClientSecrets.load(jsonFactory, new InputStreamReader(inputStream));
AppEngineDataStoreFactory appEngineDataStoreFactory =
AppEngineDataStoreFactory.getDefaultInstance();
//IS SOMETHING MISSING HERE???
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow
.Builder(httpTransport, jsonFactory, googleClientSecrets, SCOPES)
.setDataStoreFactory(appEngineDataStoreFactory)
.setAccessType("offline")
.build();
现在我正在尝试使用以下行创建用于访问 Google Drive 的凭据:
Credential credential = flow.loadCredential(USER_ID1);
那是返回null。
在我看来,我缺少根据我在上面 github 链接的示例中看到的内容将凭据分配给 AppEngineDataStoreFactory。但是,我不确定这是否是问题,如果是,我该如何解决。
是否有一种直接的方法可以使用从 UserServiceFactory.getUserService().getCurrentUser().getUserId() 获得的登录用户 ID 分配凭据?或者我应该获得 accetoken 并创建凭证?如果是这样,如何?
(我不想使用 javascript,因为它似乎不适合 web 应用程序)
任何帮助都会很棒!!!!
PS:我还想补充一点,用户只需要通过 web 或 android 访问由同一 web 应用程序添加的文件
更新 #1响应 @Aerials:这是我尝试获取 TokenResponse 的代码:
VerificationCodeReceiver receiver = new GooglePromptReceiver();
(I know above one is not the right option, but I am not able to find any other)
AuthorizationCodeRequestUrl authorizationUrl =
flow.newAuthorizationUrl().setRedirectUri(receiver.getRedirectUri());
String code = receiver.waitForCode();
(Above line returns: java.util.NoSuchElementException: No line found)
TokenResponse tokenResponse =
flow.newTokenRequest(code).setRedirectUri(redirectUri).execute();
更新 #2代码,用于获取 TokenResponse 和创建凭据并成功连接到 Google Drive 的其余任务:
GenericUrl genericUrl = new GenericUrl(request.getRequestURL().toString());
genericUrl.setRawPath("/googleDriveTest");
String redirectUri = genericUrl.build();
(redirectUri 应与 GCP API Credentials 下 OAuth ClientID 内的授权重定向 URI 匹配。如果现在添加,则需要重新下载 credentials.json 文件)
String redirectUrl = authorizationCodeFlow
.newAuthorizationUrl()
.setRedirectUri(redirectUri)
.build();
String authorizationCode = request.getParameter("code");
if (authorizationCode == null || authorizationCode.isEmpty())
response.sendRedirect(redirectUrl);
TokenResponse tokenResponse = authorizationCodeFlow
.newTokenRequest(authorizationCode)
.setRedirectUri(redirectUri).execute();
authorizationCodeFlow.createAndStoreCredential(tokenResponse, USER_ID1);
Credential credential = authorizationCodeFlow.loadCredential(USER_ID1);
Drive service = new Drive.Builder(httpTransport, jsonFactory, credential)
.setApplicationName("myapplicationname")
.build();
解决方案
您需要首先创建凭据并将其存储在流的凭据存储中:
createAndStoreCredential(TokenResponse response, String userId)
能够加载它们loadCredential(String userId)
该loadCredential()
方法将返回在给定用户 ID 的凭证存储中找到的凭证,或者返回 null 表示未找到。
推荐阅读
- javascript - 单击并拖动可排序的jQuery div时,项目从浏览器中间跳转到左侧
- cplex - Cplex:Windows 与 Linux
- acumatica - Acumatica - 写入 CRRelation 的方法在哪里?
- exim - Exim - 识别收件人密件抄送地址
- windows - ntfs 元数据 $LogFile 是否可删除?
- macros - 在 Catia 5 中捕获宏
- python - 命令行程序中函数“停止”代码中的if语句?
- qt - 无法使用 qrc 加载 .qml 和 .js 文件
- static-libraries - Xcode 10 静态库不包含 x86_64 架构
- java - 由于多个可能的 bean,SpringBoot 自动装配泛型类型失败