【问题标题】:Using Google Sheets API without authentication无需身份验证即可使用 Google Sheets API
【发布时间】:2019-06-11 18:52:57
【问题描述】:

我的任务是从 Google 表格中提取数据并将信息存储在数据库中。我已经查看了文档,对于必须创建云平台项目感到有些困惑。我试图阅读的表格没有任何用户身份验证,任何人都可以访问它们。

【问题讨论】:

  • 表格是公开的并不意味着您可以通过 API 访问它们,我认为您需要有凭据。

标签: c# .net google-api google-sheets-api google-api-dotnet-client


【解决方案1】:

如果它是公共工作表,那么您可以使用公共 API 密钥来访问它。 Google 需要知道谁在使用他们的 API。他们要求您在 Google 开发者控制台上创建一个项目并创建密钥以访问公共数据。

namespace GoogleSamplecSharpSample.Sheetsv4.Auth
{
    /// <summary>
    /// When calling APIs that do not access private user data, you can use simple API keys. These keys are used to authenticate your 
    /// application for accounting purposes. The Google API Console documentation also describes API keys.
    /// https://support.google.com/cloud/answer/6158857
    /// </summary>
    public static class ApiKeyExample
    {
        /// <summary>
        /// Get a valid SheetsService for a public API Key.
        /// </summary>
        /// <param name="apiKey">API key from Google Developer console</param>
        /// <returns>SheetsService</returns>
        public static SheetsService GetService(string apiKey)
        {
            try
            {
                if (string.IsNullOrEmpty(apiKey))
                    throw new ArgumentNullException("api Key");

                return new SheetsService(new BaseClientService.Initializer()
                {
                    ApiKey = apiKey,
                    ApplicationName = string.Format("{0} API key example", System.Diagnostics.Process.GetCurrentProcess().ProcessName),
                });
            }
            catch (Exception ex)
            {
                throw new Exception("Failed to create new Sheets Service", ex);
            }
        }
    }
}

APIKey.cs 窃取的代码

【讨论】:

    猜你喜欢
    • 2017-03-04
    • 2021-08-18
    • 2020-12-29
    • 1970-01-01
    • 1970-01-01
    • 2017-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多