F. *_*nim 1 python google-authentication google-drive-api google-console-developer google-sheets-api
Service Account Credentials 我在这里创建并得到了 json key 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)
Run Code Online (Sandbox Code Playgroud)
有错误:
<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."
}
}
Run Code Online (Sandbox Code Playgroud)
我发现没有Auth header,我就是匿名的,匿名使用的配额为零。如何设置标头或者此错误的原因是其他原因?
我想要的只是在任何计算机的 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)
Run Code Online (Sandbox Code Playgroud)
#### folderId ###,请设置与服务帐号的邮箱共享的文件夹ID。SCOPES = ["https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/spreadsheets"].| 归档时间: |
|
| 查看次数: |
4595 次 |
| 最近记录: |