【问题标题】:Google Service Account谷歌服务帐号
【发布时间】:2016-07-23 23:04:45
【问题描述】:

我正在尝试将用 C# 编写的服务器连接到我的 Google Drive,因此我使用 Google 服务帐户,我从 Google API 控制台配置了所有内容并具有以下代码

    static void Main(string[] args)
    {
        string[] Scopes = { DriveService.Scope.Drive };
        var certificate = new X509Certificate2("client_secret.p12", "notasecret", X509KeyStorageFlags.Exportable);
        try
        {
            ServiceAccountCredential credential = new ServiceAccountCredential(
                new ServiceAccountCredential.Initializer("sp-update@salmon-player-update.iam.gserviceaccount.com")
                {
                    Scopes = Scopes
                }.FromCertificate(certificate));
            DriveService service = new DriveService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = "Salmon Player Update"
            });
            FilesResource.ListRequest listRequest = service.Files.List();
            listRequest.PageSize = 10;
            listRequest.Fields = "nextPageToken, files(id, name)";

            // List files.
            IList<Google.Apis.Drive.v3.Data.File> files = listRequest.Execute()
                .Files;
            Console.WriteLine("Files:");
            if (files != null && files.Count > 0)
            {
                foreach (var file in files)
                {
                    Console.WriteLine("{0} ({1})", file.Name, file.Id);
                }
            }
            else
            {
                Console.WriteLine("No files found.");
            }
            Console.Read();
        } catch(Exception ex)
        {
            Console.WriteLine(ex.InnerException);
        }
    }

但无论我做什么,我得到的只是“入门”文件,该文件不再存在。我有很多不同的文件无法访问。 任何想法这里可能有什么问题?

【问题讨论】:

    标签: google-drive-api google-api-dotnet-client service-accounts


    【解决方案1】:

    服务帐户是它自己的用户,因此它会有自己的云端硬盘帐户空间。如果您想从您的个人帐户访问文件,则必须 a) 与您的服务帐户的电子邮件地址共享这些文件,或 b) 使用 OAuth 流程并使用您自己的用户而不是机器人服务帐户进行身份验证。

    【讨论】:

    • 但它也以某种方式绑定到我开设服务帐户的个人帐户,不是吗?
    • 以及他们如何在我的个人帐户和服务帐户之间共享驱动器空间
    • 不,无论如何它都不会绑定到您的个人帐户。如果您想共享云端硬盘空间,我建议您在您的个人云端硬盘空间中创建一个文件夹并与服务帐户“共享”它,以便它具有完全的读写权限。
    • 服务帐号与您在 Google API 控制台中创建的项目相关联,但此关联仅用于监控和计费(如果适用)您的 API 调用。
    • 除了 API 调用之外,是否有可能探索服务帐户存储,我的意思是使用 google drive UI?
    猜你喜欢
    • 2018-07-22
    • 1970-01-01
    • 2018-09-22
    • 1970-01-01
    • 1970-01-01
    • 2020-11-24
    • 2013-01-09
    • 2019-05-23
    • 2017-01-28
    相关资源
    最近更新 更多