【问题标题】:C# Google Drive Connections to 3rd PartiesC# Google Drive 与第 3 方的连接
【发布时间】:2019-12-26 00:24:55
【问题描述】:

我们有一个 C# 服务/ASP.NET 组合,我们需要定期将文件上传到 Google Drive。 设计是在 ASP.NET 项目中设置我们想要连接的对象,然后允许该服务上传文件。

我已经开始了这个项目,用 nuget 下载了所有的库,在 google 中设置了我的 clientid 和 secret,这一切看起来都很简单,但是我的问题涉及到我连接到其他人的 google drive 实例?

我知道我们需要连接到驱动器 API 并获得一个令牌,但是如果我们想连接到“johnny customers”谷歌驱动器实例……我们如何在 C# 中启动它?

提前致谢!

【问题讨论】:

  • 据我所知,如果他们给你一个 API 密钥,只要权限设置正确,你应该能够连接到他们的云端硬盘。见support.google.com/a/answer/6105699?hl=en
  • 嘿@BitWiseByteDumb,哪种驱动器实例是“johnny 客户”?它是您拥有的帐户的个人驱动器,还是Shared Drive?干杯
  • 将是共享驱动器。

标签: c# google-drive-api


【解决方案1】:

为了从您的 C# 应用程序访问您的共享驱动器,您可以使用服务帐户。此功能的想法是拥有一个您可以通过编程方式登录并从中发出请求的帐户,而无需像使用“正常” OAuth2 流程那样明确允许该应用程序。您可以详细了解如何使用和创建服务帐户here

使用服务帐户的示例 C# 代码:

using System;
using System.Security.Cryptography.X509Certificates;

using Google.Apis.Auth.OAuth2;
using Google.Apis.Plus.v1;
using Google.Apis.Plus.v1.Data;
using Google.Apis.Services;

namespace Google.Apis.Samples.PlusServiceAccount
{
    /// <summary>
    /// This sample demonstrates the simplest use case for a Service Account service.
    /// The certificate needs to be downloaded from the Google API Console
    /// <see cref="https://console.developers.google.com/">
    ///   "Create another client ID..." -> "Service Account" -> Download the certificate,
    ///   rename it as "key.p12" and add it to the project. Don't forget to change the Build action
    ///   to "Content" and the Copy to Output Directory to "Copy if newer".
    /// </summary>
    public class Program
    {
        // A known public activity.
        private static String ACTIVITY_ID = "z12gtjhq3qn2xxl2o224exwiqruvtda0i";

        public static void Main(string[] args)
        {
            Console.WriteLine("Plus API - Service Account");
            Console.WriteLine("==========================");

            String serviceAccountEmail = "SERVICE_ACCOUNT_EMAIL_HERE";

            var certificate = new X509Certificate2(@"key.p12", "notasecret", X509KeyStorageFlags.Exportable);

            ServiceAccountCredential credential = new ServiceAccountCredential(
               new ServiceAccountCredential.Initializer(serviceAccountEmail)
               {
                   Scopes = new[] { PlusService.Scope.PlusMe }
               }.FromCertificate(certificate));

            // Create the service.
            var service = new PlusService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = "Plus API Sample",
            });

            Activity activity = service.Activities.Get(ACTIVITY_ID).Execute();
            Console.WriteLine("  Activity: " + activity.Object.Content);
            Console.WriteLine("  Video: " + activity.Object.Attachments[0].Url);

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }
    }
}

在获取凭据并创建DriveService 实例后,您只需调用Files: create 端点,指定作为创建文件的父级(参见"Request body" 规范)共享驱动器的根或文件夹在共享云端硬盘中。

举个例子:

var fileMetadata = new File();
fileMetadata.Name = "My File";
fileMetadata.Parents = new List<string> { "YOUR_SHARED_DRIVE_FOLDER_ID" };


FilesResource.CreateMediaUpload request;
using (var stream = new System.IO.FileStream("your_folder/your_file",
                        System.IO.FileMode.Open))
{
    request = driveService.Files.Create(
        fileMetadata, stream);
    request.Fields = "id";
    request.Upload();
}
var file = request.ResponseBody;
Console.WriteLine("File ID: " + file.Id);

参考

您可以在此处了解有关服务帐户的更多信息:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-18
    • 2016-02-24
    • 1970-01-01
    • 2020-03-31
    • 2022-08-20
    相关资源
    最近更新 更多