![](/img/trans.png)
[英]Write to Google Drive using Service Account Credential on behalf of different owner in Python
[英]How to create Google Sheets using Service Account Credential in Python?
我在这里创建了Service Account Credentials
并获得了 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"]
。如果我误解了您的问题并且这不是您想要的方向,我深表歉意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.