我创建了Service Account Credentials
here,并获得了json键service.json
。
然后我尝试:
from google.oauth2 import service_account
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
credentials = service_account.Credentials.from_service_account_file(
'service.json', scopes=SCOPES)
drive = build('drive', 'v3', credentials=credentials)
file_metadata = {
'name': 'sampleName',
'parents': ['#### folderId ###'],
'mimeType': 'application/vnd.google-apps.spreadsheet',
}
res = drive.files().create(body=file_metadata).execute()
print(res)
出现错误:
<HttpError 403 when requesting https://www.googleapis.com/drive/v3/files?alt=json returned "Insufficient Permission: Request had insufficient authentication scopes.">
{
"error": {
"errors": [
{
"domain": "usageLimits",
"reason": "dailyLimitExceededUnreg",
"message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.",
"extendedHelp": "https://code.google.com/apis/console"
}
],
"code": 403,
"message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."
}
}
我发现没有Auth标头,我是匿名的,匿名使用的配额为零。如何设置标头,或者出现此错误的原因是其他原因?
我想要的是从任何计算机的gdrive文件夹中使用python创建电子表格,而无需单击某处来授予访问权限。
如果我的理解正确,那么这个答案如何?请认为这只是几个可能的答案之一。
https://www.googleapis.com/auth/drive
?修改脚本后,请如下进行修改。
from googleapiclient.discovery import build # Added
from google.oauth2 import service_account
SCOPES = ['https://www.googleapis.com/auth/drive'] # Modified
credentials = service_account.Credentials.from_service_account_file('service.json', scopes=SCOPES)
drive = build('drive', 'v3', credentials=credentials)
file_metadata = {
'name': 'sampleName',
'parents': ['#### folderId ###'],
'mimeType': 'application/vnd.google-apps.spreadsheet',
}
res = drive.files().create(body=file_metadata).execute()
print(res)
#### folderId ###
,请设置与服务帐户的电子邮件共享的文件夹ID。SCOPES = ["https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/spreadsheets"]
。如果我误解了你的问题,而这不是你想要的方向,我深表歉意。