[英]Google Drive API, Oauth and service account
我在 Google Drive API、服務帳戶和身份驗證方面遇到了一些問題。 我讀了很多,但我不知道如何解決這個問題。
上下文:我的 Drive 帳戶中有一些文件(大約 35GB)和一個簡單的 Web 應用程序,可讓用戶登錄,查看我的 Drive 中的一些選定文件夾/文件,並在需要時下載它們。 唯一可以直接訪問我的 Drive 帳戶的是(或應該是)我的服務器,用戶通過網絡應用程序/服務器做他們的事情。
經過一番搜索,我發現服務器到服務器的授權文檔應該非常適合我的目的,但是,正如我所看到的,服務帳戶不共享相同的驅動器空間:它們有自己的並且不可升級。 由於這個(奇怪的)限制,我無法使用服務帳戶,因為我有超過 35GB 的空間,我需要“共享”所有內容。
其他方式:使用“標准”OAuth 來獲取訪問令牌,然后使用它來調用 Drive API,但訪問令牌有一個過期日期,我不能每次都手動更新它。
那么,第一個問題:有沒有辦法增加服務帳戶的配額? 如果沒有,是否有辦法像服務帳戶一樣使用我的“普通”帳戶(所有者)?
第二個(虛擬)問題:我閱讀了有關創建新 OAuth 憑據的文檔,最后您獲得了一些示例代碼和“客戶端機密”JSON。 我運行了這個例子,但我不明白那個 JSON 文件的作用是什么:我必須登錄並授予權限,為什么我需要它?
第三個(足夠愚蠢的)問題:如果 OAuth 是唯一的解決方案,有沒有辦法獲取/刷新訪問令牌而無需每次都手動執行? 我查看了 OAuth 文檔,“用戶交互/確認”是身份驗證流程中的基本內容之一,所以我認為這是不可能的。
將您的雲端硬盤帳戶的文件夾共享到您的服務帳戶。
您的服務帳號的地址類似於 XXX@XXX.iam.gserviceaccount.com。
然后您的服務帳戶可以從您的雲端硬盤帳戶看到共享文件夾。
所以你有 35GB 的服務帳戶。
如果您的帳戶是私人帳戶而不是 Google Workspace(以前稱為 GSuite)的一部分,那么 Drinkmystery的答案是正確的方法。 否則,有一種更優雅的方法可以使用createDelegated()
方法解決此問題。 它在此處進行了描述,您應該遵循那里的所有配置說明,但是那里提供的代碼示例基於一些已棄用的軟件包,因此我花了一些時間與Drive API 教程中的代碼相結合才能使其正常工作。
所以對於那些只需要一個工作代碼示例的人來說,這里是(注意createDelegated
的使用):
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.DriveScopes;
import com.google.api.services.drive.model.File;
import com.google.api.services.drive.model.FileList;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.ServiceAccountCredentials;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Arrays;
import java.util.List;
public class DelegatedDriveWithServiceAccountQuickstart {
private static final String APPLICATION_NAME = "your awesome app";
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static final List<String> SCOPES = Arrays.asList(DriveScopes.DRIVE);
private static final String CREDENTIALS_FILE_PATH = "/path/to/your/service-account-key-downloaded-from-google-api-console.json";
public static void main(String... args) throws IOException, GeneralSecurityException {
// Build a new authorized API client service
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(ServiceAccountCredentials.fromStream(new FileInputStream(CREDENTIALS_FILE_PATH))
.createScoped(SCOPES)
.createDelegated("user.whose.drive.you.want.to.share@your-domain-in-gsuite.com"));
Drive driveService = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, requestInitializer)
.setApplicationName(APPLICATION_NAME)
.build();
// Print the names and IDs for up to 10 files.
FileList result = driveService.files().list()
.setPageSize(10)
.setFields("nextPageToken, files(id, name)")
.execute();
List<File> files = result.getFiles();
if (files == null || files.isEmpty()) {
System.out.println("No files found.");
} else {
System.out.println("Files:");
for (File file : files) {
System.out.printf("%s (%s)\n", file.getName(), file.getId());
}
}
}
}
請確保您的項目依賴項是最新的(不要盲目使用 Drive API 教程中的那些)。 這是我的 build.gradle:
apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'DelegatedDriveWithServiceAccountQuickstart'
sourceCompatibility = 1.8
targetCompatibility = 1.8
version = '1.0'
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.api-client:google-api-client:1.31.1'
compile 'com.google.apis:google-api-services-drive:v3-rev20201130-1.31.0'
compile 'com.google.auth:google-auth-library-oauth2-http:0.22.2'
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.