如何请求 Google 驱动器中的文件

How to request a File in Google Drive

我可以在 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

然后我将如何在 python 中执行此请求?我目前正在尝试使用:

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

但是我收到了 404。我该如何处理这个请求?

You have to authenticate your request。我建议使用 Google 的 python 客户端来删除大量样板文件:

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

来自文档:

from apiclient.discovery import build

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

然后使用服务:

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()