问题
USING: Windows7, Python 2.7, Google App Engine
Google's documentation for inserting(creating) a file to Google Drive using Python and the Drive API. Here is the link showing the code near the bottom of the page:
Write a file to a Google Drive using Python
A function named: insert_file
is defined in the Python module.
def insert_file(service, title, description, parent_id, mime_type, filename):
The insert_file
function takes 6 arguments passed into it. The first arg is service
.
In the comment section of the example code, it is indicated that the service
arg takes the Drive API service instance
as the input.
Args:
service: Drive API service instance. title: Title of the file to insert, including the extension. description: Description of the file to insert. parent_id: Parent folders ID. mime_type: MIME type of the file to insert. filename: Filename of the file to insert.
What is the Drive API service instance
? I have no idea what that is or what the valid settings are. Is it the authorization scope that is expressed as a URL? I do know what the title
and description
are. The title is the new name of the file being written, and the description is a detail, presumably put into the files metadata. I'm not sure how to get the parent_id
or the Parent folder
either. How is that info obtained? Do I get that manually from Google Drive? I know what the MIME type setting is.
If someone could give an explanation of what the Drive API service instance
is, and give an example, that would be great. I did a search for Drive API service instance
, and couldn't find an explanation. I searched the internet. I searched Google Developers. I found nothing.
回答1:
Quickstart provides more boilerplate and a full working walk-through.
http = httplib2.Http()
http = credentials.authorize(http)
service = build('drive', 'v2', http=http)
回答2:
The service is the API service that you want to instantiate. There are lots of services. An app can communicate with Google Maps, or Google tasks, or email, or Drive.
Google API's for Python
So, the service is the API service
. Build instantiates the API service. This is from the video, minute 12:46.
YouTube example for Google Drive API Service
I found something about Parent Folders
in the documentation.
Google Drive API
The Google Drive API has a files:insert
API. The files:insert
API makes a request with various parameters. There is, what is called, the Request body which has it's own parameters. One of the parameters for the Request Body is parents[]
. It is an optional parameter. For insert
, if the parents[]
parameter is blank, the file gets created in the users root directory. So, I guess if you want the file to be written to a particular folder, you need to give the parents[] parameter a name. I'm assuming that is what the parent_id
arg in the insert_file
function is for, but I'm not sure. I need to look at the actual function, but that's not given.
After doing searches on Parent ID
it looks like that is the folder ID. When you go to your Google Drive, and click on a folder, the URL in the browsers address field changes. Just click on the folder and the URL will look something like this:
https://drive.google.com/?tab=wo&authuser=0#folders/0B52YKjuEE44yUVZfdDNzNnR3SFE
The parentID
is the long part on the end after the forward slash.
I guess I need to look at the Google Quickstart files again.
There are at least three examples that I've found:
- Quickstart example. Google Drive SDK
- Dr Edit. Google Drive SDK examples
- Another Quickstart example Google Drive API
The first one is the simpliest. Dr Edit has the most files maybe? The last one looks like its more current? I don't know. It's kind of confusing about which example to use. The Drive SDK and the Drive API examples only deal with authorization of an account for some outside app to access a users account.
来源:https://stackoverflow.com/questions/20942626/filesinsert-google-drive-sdk-python-example-what-is-drive-api-service-ins