【问题标题】:Google Drive Api with Service Account C#带有服务帐户 C# 的 Google Drive Api
【发布时间】:2017-02-24 17:24:38
【问题描述】:

我已经为我的 API 编程,在我的应用程序中有一个服务帐户,它工作正常,所有连接、上传、下载和删除东西,但是当我以前使用用户服务时,所有文件都进入我的个人驱动器,现在它去某个地方,我认为它去谷歌云平台...... 问题是,我那里没有账号,因为你需要付费才能使用,所以,有谁知道所有这些文件的去向吗?

这是我用来进行连接调用的代码

    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;
    }

那么,谁能帮帮我?

【问题讨论】:

    标签: c# model-view-controller google-api google-drive-api


    【解决方案1】:

    正如thread 中所述,代码是相同的,如果您使用的是 Oauth2 或服务帐户,则没有区别。您可以与此tutorial 联系。同样基于此related post,如果您希望上传的文件位于您自己的云端硬盘内容中,那么您需要使用您自己的帐户凭据来访问云端硬盘 SDK。这不需要涉及用户交互。您只需获取一次刷新令牌,然后使用该令牌生成 Drive 的访问令牌。希望这会有所帮助!

    【讨论】:

    • 非常感谢 m8,我很担心。你真的帮了我:D
    猜你喜欢
    • 1970-01-01
    • 2021-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-29
    • 2014-02-20
    • 1970-01-01
    相关资源
    最近更新 更多