簡體   English   中英

帶有服務帳戶C#的Google Drive Api

[英]Google Drive Api with Service Account C#

我已經編程到我的API,在我的應用程序中有一個服務帳戶,它可以正常工作,可以進行所有連接,上載,下載和刪除內容,但是當我以前使用User Service時,所有文件都移到了我的個人驅動器上,現在它移到了某個地方,我認為它去了Google Cloud Platform ...問題是,我那里沒有任何帳戶,因為您需要付費才能使用它,所以,有人知道所有這些文件的去向嗎?

這是我用來進行連接呼叫的代碼

    public static DriveService Connection(string path, string username, string p12Path)
    {

        var certificate = new X509Certificate2(p12Path, "XXXXXXXX", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable);

        ServiceAccountCredential credential = new ServiceAccountCredential(
            new ServiceAccountCredential.Initializer("g service account")
            {
                Scopes = Scopes
            }.FromCertificate(certificate));

        DriveService service = new DriveService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = ApplicationName
        });

        return service;
    }

這是我用來上傳文件的方法。

    [Authorize]
    public static Google.Apis.Drive.v3.Data.File Upload(DriveService service, string uploadFile, string name)
    {

        var body = new Google.Apis.Drive.v3.Data.File();
        body.Name = name;
        body.MimeType = GetMimeType(uploadFile);

        byte[] byteArray = System.IO.File.ReadAllBytes(uploadFile);
        System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
        FilesResource.CreateMediaUpload request = service.Files.Create(body, stream, GetMimeType(uploadFile));
        request.Upload();
        return request.ResponseBody;
    }

那么,有人可以幫助我嗎?

如該線程中所述,代碼是相同的,並且如果您使用的是Oauth2或服務帳戶,則沒有區別。 您可以閱讀教程 同樣基於此相關文章 ,如果您希望上傳的文件包含在自己的雲端硬盤內容中,則需要使用自己的帳戶憑據訪問雲端硬盤SDK。 這不需要涉及用戶交互。 您只需要一次性獲取刷新令牌,然后使用該令牌來生成Drive的訪問令牌。 希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM