代码之家  ›  专栏  ›  技术社区  ›  Jack

Google drive-更改不带oauth的共享电子表格

  •  1
  • Jack  · 技术社区  · 8 年前

    我想在谷歌硬盘上编程编辑一个共享的电子表格。 我在用C#

    当用户单击一个按钮时,我的代码应该使用公司开发人员帐户+密码来访问google驱动器上的电子表格并更新日期字段。这就是它所需要做的。

    在我看来,oAuth需要用户自己向google进行身份验证,或者至少这是我从google.Apis.Auth.OAuth2AuthorizeAsync()得到的印象

        //
        // Summary:
        //     Asynchronously authorizes the specified user. Requires user interaction; see
        //     Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker remarks for more details.
        //
    

    这不是我需要或想要的,但围绕这一点的文档似乎完全不透明。。。也许我在这里遗漏了什么? 有人知道另一种方法吗?

    1 回复  |  直到 8 年前
        1
  •  1
  •   Linda Lawton - DaImTo    8 年前

    为了更新任何需要验证的内容,即使工作表设置为public并且任何人都可以访问它。从程序上讲你仍然需要被认证。

    你应该考虑使用一个服务帐户。如果使用服务帐户电子邮件地址与服务帐户共享工作表,则服务帐户类似于虚拟用户。这样它就可以访问该工作表,而无需经过身份验证。

    public static class ServiceAccountExample
    {
    
        /// <summary>
        /// Authenticating to Google using a Service account
        /// Documentation: https://developers.google.com/accounts/docs/OAuth2#serviceaccount
        /// </summary>
        /// <param name="serviceAccountEmail">From Google Developer console https://console.developers.google.com</param>
        /// <param name="serviceAccountCredentialFilePath">Location of the .p12 or Json Service account key file downloaded from Google Developer console https://console.developers.google.com</param>
        /// <returns>AnalyticsService used to make requests against the Analytics API</returns>
        public static SheetsService AuthenticateServiceAccount(string serviceAccountEmail, string serviceAccountCredentialFilePath, string[] scopes)
        {
            try
            {
                if (string.IsNullOrEmpty(serviceAccountCredentialFilePath))
                    throw new Exception("Path to the service account credentials file is required.");
                if (!File.Exists(serviceAccountCredentialFilePath))
                    throw new Exception("The service account credentials file does not exist at: " + serviceAccountCredentialFilePath);
                if (string.IsNullOrEmpty(serviceAccountEmail))
                    throw new Exception("ServiceAccountEmail is required.");                
    
                // For Json file
                if (Path.GetExtension(serviceAccountCredentialFilePath).ToLower() == ".json")
                {
                    GoogleCredential credential;
                    using (var stream = new FileStream(serviceAccountCredentialFilePath, FileMode.Open, FileAccess.Read))
                    {
                        credential = GoogleCredential.FromStream(stream)
                             .CreateScoped(scopes);
                    }
    
                    // Create the  Analytics service.
                    return new SheetsService(new BaseClientService.Initializer()
                    {
                        HttpClientInitializer = credential,
                        ApplicationName = "Sheets Service account Authentication Sample",
                    });
                }
                else if (Path.GetExtension(serviceAccountCredentialFilePath).ToLower() == ".p12")
                {   // If its a P12 file
    
                    var certificate = new X509Certificate2(serviceAccountCredentialFilePath, "notasecret", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable);
                    var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail)
                    {
                        Scopes = scopes
                    }.FromCertificate(certificate));
    
                    // Create the  Sheets service.
                    return new SheetsService(new BaseClientService.Initializer()
                    {
                        HttpClientInitializer = credential,
                        ApplicationName = "Sheets Authentication Sample",
                    });
                }
                else
                {
                    throw new Exception("Unsupported Service accounts credentials.");
                }
    
            }
            catch (Exception ex)
            {                
                throw new Exception("CreateServiceAccountSheetsFailed", ex);
            }
        }
    }
    

    ServiceAccount.cs