Craig
Craig

Reputation: 1225

Using Google Sheets API without authentication

I have been tasked with pulling data from Google Sheets and storing the information in a database. I have reviewed the documentation and I am a bit confused about having to create a cloud platform project. The sheets that I am attempting to read do not have any user authentication, anybody has access to them.

Upvotes: 2

Views: 1636

Answers (1)

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 116868

If it's a public sheet then you can use a public API key to access it. Google needs to know who is using their API. They do that by requiring that you create a project on google developer console and create keys to access even public data.

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);
            }
        }
    }
}

code ripped from APIKey.cs

Upvotes: 2

Related Questions