无法使用Google Docs API复制Google Doc(无'copy'属性)

问题描述 投票:0回答:1

我似乎无法从Google文档中复制文档,但是我可以编辑已经创建的文档,也可以创建空白文档。使用下面的代码,我得到AttributeError:'Resource'对象没有属性'copy'。谁能提供一些见识?复制代码位于:https://developers.google.com/docs/api/how-tos/documents,入门代码位于:https://developers.google.com/docs/api/quickstart/python

from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request

# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/documents', 'https://www.googleapis.com/auth/drive.file']

# The ID of a sample document.
document_id = f'{sample_document_ID}'

def main():
    """Shows basic usage of the Docs API.
    Prints the title of a sample document.
    """
    creds = None
    # The file token.pickle 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.pickle'):
        with open('token.pickle', 'rb') as token:
            creds = pickle.load(token)
    # 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(
                'googleDocsCredentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)

    service = build('docs', 'v1', credentials=creds)


    copy_title = 'Copy Title'
    body = {
        'name': copy_title
    }
    drive_response = service.documents().copy(
        fileId=document_id, body=body).execute()
    document_copy_id = drive_response.get('id')

if __name__ == '__main__':
    main()
python google-docs-api
1个回答
0
投票

这个答案怎么样?

修改点:

为了复制Google Docs文件,需要使用Drive API的files().copy方法。 Ref我认为您的问题的原因是此。

这样,您的脚本可以进行如下修改。

修改的脚本:

从:
service = build('docs', 'v1', credentials=creds)


copy_title = 'Copy Title'
body = {
    'name': copy_title
}
drive_response = service.documents().copy(
    fileId=document_id, body=body).execute()
document_copy_id = drive_response.get('id')
至:
service = build('drive', 'v3', credentials=creds)  # Modified

document_id = '###'  # Please set the Document ID.
copy_title = 'Copy Title'
body = {
    'name': copy_title
}
drive_response = service.files().copy(   # Modified
    fileId=document_id, body=body).execute()
document_copy_id = drive_response.get('id')
print(document_copy_id)

注意:

  • 根据您的情况,使用https://www.googleapis.com/auth/drive.file的范围。如果在运行脚本时甚至在将Google文档放入Google云端硬盘时发生File not found错误,请将https://www.googleapis.com/auth/drive.file修改为https://www.googleapis.com/auth/drive,然后删除token.pickle文件。并且,请再次重新授权范围。这样,可以使用新的作用域。

参考:

© www.soinside.com 2019 - 2024. All rights reserved.