Java : Google AppEngine : OAuth : Google 驱动器 API 以访问用户的 Google 驱动器由 Web 应用程序创建的内容

Java : Google AppEngine : OAuth : Google Drive API to access user's Google Drive contents created by Web Application

场景:我正在 Google AppEngine 上开发 Web 应用程序,用户必须从他们自己的 Google 驱动器访问文件。我已经寻找在线帮助,这就是我想出来的。

我已经使用这个 link 作为帮助,在使用本地机器进行测试时效果很好 https://github.com/gsuitedevs/java-samples/blob/master/drive/quickstart/src/main/java/DriveQuickstart.java

步骤 1(看似简单):启用 Google 驱动器 API 并设置必要的凭据,即 Web 应用程序的 OAuth 2.0 客户端 ID。 (这是在 Google AppEngine 上启用 IAP 时完成的,所以我没有再做一次。每当有人打开 Web 应用程序时,都会要求 he/she 通过 iap.googleapis.com 进行身份验证并保存详细信息。这工作正常)。此外,我已将 Google Drive Scopes 添加到 OAuth 同意屏幕(../auth/drive.appdata & ../auth/drive.file),不需要 Google 验证.

步骤 2:从 OAuth 客户端 ID 下载 credentials.json 并存储在应用程序包根目录中创建的“resources”文件夹中(在主文件夹中,在java 和 webapp 文件夹)

第 3 步:我创建了一个测试 class (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 驱动器的凭据:

Credential credential = flow.loadCredential(USER_ID1);

返回 null。

在我看来,根据我在上面 github link 的示例中看到的内容,我没有将凭据分配给 AppEngineDataStoreFactory。但是,我不确定这是否是问题所在,如果是,我该如何解决。

是否有直接的方法使用从 UserServiceFactory.getUserService().getCurrentUser().getUserId() ?还是我应该获取 accetoken 并创建凭证?如果可以,怎么做?

(我不想使用 java 脚本,因为它似乎不适合 Web 应用程序)

任何帮助都会很棒!!!!

PS:我还想补充一点,用户只需通过网络或从 android

访问由同一网络应用程序添加的文件

更新 #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 驱动器的代码:

GenericUrl genericUrl = new GenericUrl(request.getRequestURL().toString());
genericUrl.setRawPath("/googleDriveTest");
String redirectUri = genericUrl.build();

(redirectUri 应与 GCP API 凭据下 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() 方法将 return 在给定用户 ID 的 凭据存储区 中找到的凭据,或 none 找到的空值。