[英]Java : Google AppEngine : OAuth : Google Drive API to access user's Google Drive contents created by Web Application
場景:我正在 Google AppEngine 上開發 Web 應用程序,用戶必須從他們自己的 Google Drive 訪問文件。 我一直在尋找在線幫助,這就是我想出的。
我已使用此鏈接尋求幫助,在使用本地機器https://github.com/gsuitedevs/java-samples/blob/master/drive/quickstart/src/main/java/DriveQuickstart.java進行測試時效果很好
第 1 步(看似簡單):啟用 Google Drive API 並設置必要的憑據,即 OAuth 2.0 客戶端 ID 用於 Web 應用程序。 (這是在 Google AppEngine 上啟用 IAP 時完成的,所以我沒有再這樣做。每當有人打開 web 應用程序時,都會要求他/她通過 iap.googleapis.com 進行身份驗證並保存詳細信息。這工作正常)。 此外,我已將 Google Drive Scopes 添加到不需要 Google 驗證的 OAuth 同意屏幕 (../auth/drive.appdata &../auth/drive.file)。
Step 2 : Downloaded the credentials.json from OAuth Client ID and stored inside "resources" folder created in the root of application package (inside main folder, next to java and webapp folders)
第 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 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代碼,用於獲取創建憑據並成功連接到 Google Drive 的任務的 TokenResponse 和 rest:
GenericUrl genericUrl = new GenericUrl(request.getRequestURL().toString());
genericUrl.setRawPath("/googleDriveTest");
String redirectUri = genericUrl.build();
(redirectUri 應與 GCP API Credentials 下 OAuth ClientID 內的授權重定向 URI 匹配。如果現在添加,則需要重新下載 credentials.Z466DEEC76ECDF5FCA6D38571F6324D54 文件)
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 (未找到)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.