How to use the scope https://www.googleapis.com/auth/drive.file correctly

吃可爱长大的小学妹 提交于 2020-06-27 08:47:11

问题


I have tried accessing a Google Sheets file in my Google Drive with this code:

import gspread
from oauth2client.service_account import ServiceAccountCredentials

scope = ['https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name('credentials.json', scope)

gc = gspread.authorize(credentials)

wks = gc.open('my_test').sheet1

print(wks.get_all_records())

I have then created a spreadsheet called my_test in my drive and shared it with the email in credentials.json. It then worked. The scope, however, is too broad and I'd like to use

scope = ['https://www.googleapis.com/auth/drive.file']

instead. But if I change the scope, it cannot find the sheet:

File "/home/my_username/.local/lib/python3.6/site-packages/gspread/client.py", line 130, in open
    raise SpreadsheetNotFound
gspread.exceptions.SpreadsheetNotFound

Why is that? How can I change it? I have googled around a lot and for most people sharing or resharing the spreadsheet worked. In my case the problem persists.

I also tried this Error 500 when performing a query with drive.file scope solution of adding a scope for Drive metadata, but that leads to this permissions error, which could be seen as progress (since it finds the file):

gspread.exceptions.APIError: {
  "error": {
    "code": 403,
    "message": "The caller does not have permission",
    "status": "PERMISSION_DENIED"
  }
}

However, this error persists even after I set the spreadsheet access to 'public'.

Any hints greatly appreciated!

Oh, and I have a more general question: does the Service Account have access to MY entire drive? Or only to everything that was shared with the Service Account? Cause I have only shared one spreadsheet with it, I am a bit confused on how exactly the permissions work here.

来源:https://stackoverflow.com/questions/51571487/how-to-use-the-scope-https-www-googleapis-com-auth-drive-file-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!