简体   繁体   中英

Google Sheets API Won't Authorize - Python

Hi StackOverflow Community,

Thanks in advance for your help.

I have set up a Python script to make some requests to the Google Sheets API. The code for my script is below:

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()

The problem is that when I run this I get the following error:

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'

The full code of this file listed in the error is located here: https://github.com/google/google-api-python-client/blob/master/googleapiclient/_auth.py

I'm working on this for an assignment and can't figure out why this error is occurring. Please help!

Thanks,

evank28

I had problems with this. This link should help: https://medium.com/@rqaiserr/how-to-connect-to-google-sheets-with-python-83d0b96eeea6

Make sure to also read this: https://www.twilio.com/blog/2017/02/an-easy-way-to-read-and-write-to-a-google-spreadsheet-in-python.html

Just note that you need to add this line of code to your project after following the instructions above:

from oauth2client.service_account import ServiceAccountCredentials

and also run this in terminal:

pip install oauth2client

The code will look something like this:

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)

Also make sure that you share the client_email with editing access (you will see what I mean after reading the articles).

If you have any questions, feel free to reply to me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM