使用 Google 张 API 未经身份验证

Using Google Sheets API without authentication

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

如果它是 public sheet 那么您可以使用 public API 键来访问它。 Google 需要知道谁在使用他们的 API。他们通过要求您在 google 开发人员控制台上创建一个项目并创建密钥来访问甚至 public 数据来做到这一点。

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

中窃取的代码