You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python操作Google Sheets时遭遇权限不足问题求助

Fixing "Insufficient Permission" with Google Sheets + Python

Hey there, let's work through that permission error you're facing. This is a super common issue when setting up gspread with service accounts, and there are a few key fixes to try:

1. Update Your API Scope

Your current scope is too limited for modern Google Sheets API interactions. Replace your scope with these broader, recommended ones:

scope = [
    'https://www.googleapis.com/auth/spreadsheets',  # Full access to Google Sheets
    'https://www.googleapis.com/auth/drive'         # Access to Google Drive files
]

The original https://spreadsheets.google.com/feeds is an older scope that doesn't cover all necessary permissions for the latest API endpoints.

2. Share Your Sheet with the Service Account Email

Service accounts are separate from your personal Google account—you need to explicitly grant them access to your sheet:

  • Open your client_secret.json file and find the client_email field (it looks like your-service-account@your-project.iam.gserviceaccount.com).
  • Open your Google Sheets document, click the Share button in the top-right.
  • Paste that service account email into the sharing field, and grant it either Editor (if you need to write to the sheet) or Viewer (if you only need read access) permissions.
  • Click Send to confirm.

3. Enable Required APIs in Google Cloud Console

Make sure both the Google Sheets API and Google Drive API are enabled for your project:

  • Go to the Google Cloud Console, select your project.
  • Navigate to APIs & Services > Library.
  • Search for "Google Sheets API" and ensure it's marked as Enabled.
  • Repeat the same for "Google Drive API".

4. Verify Your Credentials File Path

Double-check that client_secret.json is in the same directory as your Python script, or use an absolute path to the file (e.g., '/Users/yourname/projects/client_secret.json') to avoid accidental path issues.

Updated Working Code Example

Here's your code with the fixes applied:

import gspread
from oauth2client.service_account import ServiceAccountCredentials

# Updated scope with full permissions
scope = [
    'https://www.googleapis.com/auth/spreadsheets',
    'https://www.googleapis.com/auth/drive'
]

creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
client = gspread.authorize(creds)

# Open the workbook and access the first sheet
sheet = client.open("Your Sheet Name Here").sheet1

# Test with a simple read operation
print("A1 cell value:", sheet.cell(1, 1).value)

If you still run into issues, check that your service account has the correct IAM roles in the Google Cloud Console (e.g., the Editor role for your project will cover most use cases).

内容的提问来源于stack exchange,提问作者M. Freberg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:14:27