我正在撰寫一個 Python 應用程式以通過 google docs API 自動化發票,但我的代碼有錯誤,我不知道如何解決它
# [START docs_quickstart]
from __future__ import print_function
import os.path
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
# If modifying these scopes, delete the file token.json.
SCOPES = ['https://www.googleapis.com/auth/documents.readonly','https://www.googleapis.com/auth/documents','https://www.googleapis.com/auth/drive.file','https://www.googleapis.com/auth/drive','https://www.googleapis.com/auth/drive.readonly']
def main():
"""Shows basic usage of the Docs API.
Prints the title of a sample document.
"""
creds = None
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.json', 'w') as token:
token.write(creds.to_json())
try:
service = build('docs', 'v1', credentials=creds)
title = 'My Document'
body = {
'title': title
}
document = service.documents().create(body=body).execute()
print('Created document with title: {0}'.format(
document.get('title')))
except HttpError as err:
print(err)
if __name__ == '__main__':
main()
# [END docs_quickstart]
這是我遇到的錯誤代碼
PS D:\Universidad\Proyectos de Programacion Propia\python\googledocsinvoice.restapi> python quickstart.py
<HttpError 403 when requesting https://docs.googleapis.com/v1/documents?alt=json returned "Request had insufficient authentication scopes.". Details: "[{'@type': 'type.googleapis.com/google.rpc.ErrorInfo', 'reason': 'ACCESS_TOKEN_SCOPE_INSUFFICIENT', 'domain': 'googleapis.com', 'metadata': {'service': 'docs.googleapis.com', 'method': 'google.apps.docs.v1.DocumentsService.CreateDocument'}}]">
如果您知道一些使用 Google Docs API 自動創建檔案的指南或完整教程(例如發票)并替換 {{productID}} 等封裝值,我將非常感激我看到了 Google 頻道的自動檔案創建,但它不是很有幫助https://youtu.be/-dX-fWb3ogE
最好的祝福
uj5u.com熱心網友回復:
確保您還在 Developers Console 中啟用了 Docs API。
洗掉憑證檔案
~/.credentials.json和token.json(如果您之前運行過代碼更改用于閱讀檔案的范圍變數
var SCOPES = ['https://www.googleapis.com/auth/documents.readonly'];
到
var SCOPES = ['https://www.googleapis.com/auth/documents'];
查看更多資訊https://developers.google.com/docs/api/reference/rest/v1/documents/create#authorization-scopes和https://developers.google.com/docs/api/how-tos/授權
- 執行代碼后,API 將再次進行身份驗證,然后問題應該得到解決。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/422841.html
標籤:
上一篇:自定義過期mongodb中的檔案
