AttributeError: 'str' object has no attribute 'request'

AttributeError: 'str' object has no attribute 'request'

我能够成功建立连接而没有任何错误,但是当我尝试从 sheet 读取数据时,我 运行 进入 AttributeError

import os
from googleapiclient import discovery
import pandas as pd

credential = 'reference/config.json'
api_name = 'sheets'
api_version = 'v4'
scope = ['https://www.googleapis.com/auth/spreadsheets']

service = discovery.build('sheets','v4',credentials=credential)

sheet_id = [sheet_id]
ranges = 'Sheet1!D5:J36'

sheet = service.spreadsheets()
request = sheet.values().get(spreadsheetId=sheet_id, range=ranges, majorDimension='ROWS')
response = request.execute()

AttributeError: 'str' object has no attribute 'request' 出现在倒数第二行(request = sheet.values().get( spreadsheetId=sheet_id, range=ranges, majorDimension='ROWS')

我完全按照文档页面上提供的说明进行操作 https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/get

以前我没有使用授权来让我的凭据正常工作,在下面的代码中我添加了授权部分,然后添加了凭据以使代码正常工作

from __future__ import print_function
import os.path
from googleapiclient.discovery import build
from google.oauth2 import service_account

SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']
SERVICE_ACCOUNT_FILE = 'config.json'
#config.json is your GCP Service Account Credential file which gets downloaded

creds = None
creds = service_account.Credentials.from_service_account_file(
        SERVICE_ACCOUNT_FILE, scopes=SCOPES)

# The ID and range of a sample spreadsheet.
SAMPLE_SPREADSHEET_ID = '[SpreadsheetID]'
SAMPLE_RANGE_NAME = '[SheetName]![Range]'

service = build('sheets', 'v4', credentials=creds)

# Call the Sheets API
sheet = service.spreadsheets()
result = sheet.values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID,
                                range=SAMPLE_RANGE_NAME).execute()
#values = result.get('values', [])
print(result)