我可以在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.我该怎么做这个请求?
答案 0 :(得分:1)
You have to authenticate your request。我建议使用谷歌的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()