1
votes

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.

3
Are you able to use other Python modules? I had a lot of success interacting with Google Drive through pydrive.asongtoruin
I would avoid dependencies. I'm looking for HTTP request/response solution as stackoverflow.com/questions/23594515/…Enrico Ferreguti

3 Answers

1
votes

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.

0
votes

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'

0
votes

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.