Google表格API赢得了授权 - Python

时间:2017-04-07 23:51:53

标签: python-3.x authentication google-api google-sheets-api

您好StackOverflow社区,

提前感谢您的帮助。

我已经设置了一个Python脚本来向Google表格API发出一些请求。我脚本的代码如下:

from googleapiclient import discovery
credentials ='https://www.googleapis.com/auth/spreadsheets' #Should give read/write scope to my spreadsheets
service = discovery.build('sheets', 'v4', credentials=credentials) 

spreadsheet_id ='1z2QzPf9Kc02roOwTJUYab4k2dwYu1n-nIbJ5yzWF3YE' #COPY
ranges=['A1:C10']
include_grid_data = False
request = service.spreadsheets().get(spreadsheetId=spreadsheet_id,
ranges=ranges, includeGridData=include_grid_data)
response = request.execute()

问题在于,当我运行此操作时,我收到以下错误:

File "C:\Users\evank\AppData\Local\Programs\Python\Python36-32\Lib\site-packages\googleapiclient\_auth.py", line 92, in authorized_http
 return credentials.authorize(build_http())

builtins.AttributeError: 'str' object has no attribute 'authorize'

错误中列出的此文件的完整代码位于:https://github.com/google/google-api-python-client/blob/master/googleapiclient/_auth.py

我正在为此工作进行分配,并且无法弄清楚为什么会出现此错误。请帮忙!

谢谢,

evank28

1 个答案:

答案 0 :(得分:0)

我对此有疑问。 该链接应该有帮助:https://medium.com/@rqaiserr/how-to-connect-to-google-sheets-with-python-83d0b96eeea6

确保还阅读以下内容:https://www.twilio.com/blog/2017/02/an-easy-way-to-read-and-write-to-a-google-spreadsheet-in-python.html

请注意,您需要按照上述说明将以下代码行添加到项目中:

from oauth2client.service_account import ServiceAccountCredentials

并在终端中运行它:

pip install oauth2client

代码将如下所示:

from oauth2client.service_account import ServiceAccountCredentials
import gspread

scope = ['https://spreadsheets.google.com/feeds']
creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
client = gspread.authorize(creds)

sheet = client.open_by_url("spreadsheetlink.com")
worksheet = sheet.get_worksheet(0)

还要确保您共享具有编辑权限的client_email(阅读文章后您将明白我的意思)。

如果您有任何疑问,请随时回复我。