# Setting up a Service Account

### Part 1: Creating a GCP Service Account

#### Step 1: Access Google Cloud Console

1. Navigate to [Google Cloud Console](https://console.cloud.google.com/)
2. Sign in with your Google account
3. Select an existing project or create a new one from the project drop-down at the top of the page

#### Step 2: Navigate to Service Accounts

1. In the left sidebar, click on **IAM & Admin**
2. Select **Service Accounts** from the sub menu
3. You'll see a list of existing service accounts (if any)

#### Step 3: Create a New Service Account

1. Click the **+ CREATE SERVICE ACCOUNT** button at the top
2. Fill in the service account details:
   * **Service account name**: Choose a descriptive name (e.g., "sheets-data-extractor")
   * **Service account ID**: Auto-generated based on the name (you can customize it)
   * **Description**: Optional but recommended (e.g., "Service account for extracting Google Sheets data")
3. Click **CREATE AND CONTINUE**

#### Step 4: Grant Permissions (Optional)

For basic Google Sheets access, you can skip this step by clicking **CONTINUE**, then **DONE**. The service account doesn't need project-level permissions to access sheets that are explicitly shared with it.

***

### Part 2: Obtaining the Service Account Email

After creating the service account, you'll be returned to the Service Accounts list page.

1. Locate your newly created service account in the list
2. The **Email** column displays the service account email address
   * Format: `service-account-name@project-id.iam.gserviceaccount.com`
3. **Copy this email address** - you'll need it to share your Google Sheet

#### Important: Share Your Google Sheet

To allow the service account to access your Google Sheet:

1. Open your Google Sheet
2. Click the **Share** button
3. Paste the service account email address
4. Set appropriate permissions (typically "Viewer" for read-only, "Editor" for write access)
5. Uncheck "Notify people" since it's a service account
6. Click **Share**

***

### Part 3: Creating and Downloading the Service Account Key

#### Step 1: Access Key Management

1. In the Service Accounts list, click on your service account name
2. Navigate to the **KEYS** tab at the top
3. Click **ADD KEY** drop-down
4. Select **Create new key**

#### Step 2: Generate the Key

1. A dialog will appear asking for the key type
2. Select **JSON** (recommended format)
3. Click **CREATE**

#### Step 3: Secure Your Key

1. The JSON key file will automatically download to your computer
2. **IMPORTANT**: Store this file securely
   * Never commit it to version control (add to `.gitignore`)
   * Don't share it publicly
   * This key provides full access to resources the service account can access
3. Rename the file to something memorable (e.g., `sheets-service-account-key.json`)

#### Key File Structure

The downloaded JSON file contains:

```json
{
  "type": "service_account",
  "project_id": "your-project-id",
  "private_key_id": "key-id",
  "private_key": "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n",
  "client_email": "service-account-name@project-id.iam.gserviceaccount.com",
  "client_id": "client-id",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "cert-url"
}
```

***

### Part 4: Enabling the Google Sheets API

#### Step 1: Navigate to APIs & Services

1. In the GCP Console left sidebar, click **APIs & Services**
2. Select **Library** from the sub menu

#### Step 2: Search for Google Sheets API

1. In the search bar at the top, type "Google Sheets API"
2. Click on **Google Sheets API** from the search results

#### Step 3: Enable the API

1. Click the **ENABLE** button
2. Wait a few seconds for the API to be enabled
3. You'll be redirected to the API dashboard showing the API is now enabled

#### Verification

You can verify the API is enabled by:

1. Going to **APIs & Services** > **Enabled APIs & services**
2. Confirming "Google Sheets API" appears in the list
