David542
David542

Reputation: 110143

How to request a File in Google Drive

I am able to make a request using the following Request in Google API Explorer:

GET https://www.googleapis.com/drive/v2/files/asdf?key={YOUR_API_KEY}

Authorization:  Bearer ya29.EQEo6DJmR0Z-FngYc9nAL5iiidB8TBI7ysBDD6TARiqMtFjmMagLew0oC-vxj9HjojXjja46-5LSEQ
X-JavaScript-User-Agent:  Google APIs Explorer

How would I then do this request in python? I am currently trying to do it with:

api_key = '1122d'
requests.get('https://www.googleapis.com/drive/v2/files/asdf?key=%s' % api_key)

But I am getting a 404. How would I do this request?

Upvotes: 0

Views: 1019

Answers (1)

Meistro
Meistro

Reputation: 3794

You have to authenticate your request. I would recommend using Google's python client to remove a lot of the boilerplate:

pip install --upgrade google-api-python-client

From the docs:

from apiclient.discovery import build

def build_service(credentials):
  http = httplib2.Http()
  http = credentials.authorize(http)
  return build('drive', 'v2', http=http)

Then use the service:

from apiclient import errors
try:
  service = build_service(### Credentials here ###)
  file = service.files().get(fileId=file_id).execute()

  print 'Title: %s' % file['title']
  print 'Description: %s' % file['description']
  print 'MIME type: %s' % file['mimeType']
except errors.HttpError, error:
  if error.resp.status == 401:
    # Credentials have been revoked.
    # TODO: Redirect the user to the authorization URL.
    raise NotImplementedError()

Upvotes: 1

Related Questions