1

I'm struggled with Google Drive REST API interface. I need to create a folder programmatically. Reading api documents (https://developers.google.com/drive/v3/reference/files/create) it's possible to create a folder with a POST method to https://www.googleapis.com/drive/v3/files, a request body with folder name and mime type as 'application/vnd.google-apps.folder'

so I write this python function:

    def createFolder(self,folderName):
        if not self.authorization:
            self.get_authorization()
        url = 'https://www.googleapis.com/drive/v3/files'
        headers = { 'Authorization':'Bearer {}'.format(self.access_token)}
        metadata = {
            "name": folderName,
            "mimeType": 'application/vnd.google-apps.folder'
        }
        response = requests.post( url, headers = headers, params = metadata)
        return response.json()

that outputs a response object like this:

{
  u'mimeType': u'application/json', 
  u'kind': u'drive#file', 
  u'id': u'0B350e2U7rvyvR0k3NjJmTTVuWUE', 
  u'name': u'Untitled'
}

A file is created, but the folder metadata are not applied. When I do the same with "Try it!" APIs Explorer I get a correct behaviour, so I can't understand where my code is wrong.

I'm writing a portable plugin and I don't want to deal with google library so I would prefer a simple Http approach.

I'll appreciate if you can give me any suggestions.

2
  • 1
    Are you able to use other Python modules? I had a lot of success interacting with Google Drive through pydrive. Commented Dec 6, 2016 at 16:18
  • I would avoid dependencies. I'm looking for HTTP request/response solution as stackoverflow.com/questions/23594515/… Commented Dec 6, 2016 at 16:35

3 Answers 3

2

Thanks. I finally got it: (SOLVED)

    def createFolder(self,folderName):
        if not self.authorization:
            self.get_authorization()
        url = 'https://www.googleapis.com/drive/v3/files'
        headers = { 
            'Authorization':'Bearer {}'.format(self.access_token), 
            'Content-Type': 'application/json'
        }
        metadata = {
            'name': folderName,
            'mimeType': 'application/vnd.google-apps.folder'
        }
        response = requests.post( url, headers = headers, data = json.dumps(metadata))
        return response.json()

Google Drive API wants the needed parameters in {request body}, so metadata must be passed as json string and header "content-type" carefully set to "application/json", otherwise the API will not like very much python requests default to 'application/x-www-form-urlencoded'

Sign up to request clarification or add additional context in comments.

3 Comments

has this changed now? this does not work. the metadata is ignored and it creates a file with Untitled as the file name.
I confirm that the answer is still valid. I don't get any issue creating folder. Please check authorizazion token
i got this to work now. forgot about this post. my remaining issue is how to refresh the token to keep it signed in. got no luck finding any good posts.
1

There is a problem with your URL. Lose the ?uploadType=multipart as this isn't appropriate for creating a folder - "upload" is a reference to a file's content and a folder has no content.

2 Comments

Yes, but even deleting the typo the reported issue persists.
I suspect it's something to do with how requests.post is composing your POST data. You've used "Try It" so you know what should be going up the wire. Compare that with what your Python app is actually sending. Remember the POST body must be a JSON object. The fact that the file is called "Untitled" also suggests that Google Drive isn't happy about your POST body and so is ignoring it.
0

Have you tried using postman to send rest API POST call? I work with rest API and python all day long. I first test it with postman. If that works, just have postman convert it to Python code. From there, you can create your variables, create your function. Run your python script and verify the folder was created.

1 Comment

I found that issue is related to 'Content-Type' mimetype that has to be set to "application/json". Have a look to last answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.