簡體   English   中英

我的 Python 代碼與谷歌文檔 api 連接的問題

[英]problems with my Python code connecting with google docs api

我正在編寫一個 Python 應用程序以通過谷歌文檔 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

此致

  1. 確保您還在開發者控制台中啟用了文檔 API。

  2. 刪除憑證文件~/.credentials.jsontoken.json (如果你之前運行過代碼

  3. 更改用於閱讀文檔的 scope 變量

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-scopeshttps://developers.google.com/docs/api/how-tos/的更多信息授權

  1. 執行代碼后,API 將再次進行身份驗證,然后問題應該得到解決。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM