How do I get the ID of a document by knowing the file name?

Viewed 97

I am trying to get the ID of a file that I have created because I want to move it to a different folder. This is my current code attempting to get the ID:

@app.route('/gdocs/movetofolder', methods=['GET', 'POST'])
def move_doc():
    drive_id = google_drive.build_drive_api_v3()
    file_id = drive_id.files().get(fileId='My Test Doc').execute()
    print(type(file_id))
    print(file_id[id])
    return file_id
where
def build_drive_api_v3():
    credentials = build_credentials()
    return googleapiclient.discovery.build('drive', 'v3', credentials=credentials).files()

The credential getting is not the problem as I have another function that uses the credentials to upload files. Running the flask server and navigating to the correct URL gives me an AttributeError: 'Resource' object has no attribute 'files'. Any help? The code is repurposed from here ( the highest answer). Any help?

1 Answers

The best way to get a file id for a file would be to use the file.list method and the q paramater.

response = drive_service.files().list(q="name='filename'",
                                      fields='files(id, name)'
                                      ).execute()

The response will contain the id of the file.

If you inserted the file with your application then you sould know when creating a file you can directly set the directory the file is created in though the metadata.

Also the response returned from a file.create will return the id of the file there so there would be no need to search for it.

Related