2015-02-06 2 views
0
내가 구글 API 탐색기에서 다음 요청하여 요청을 할 수 있어요

:어떻게 Google 드라이브에서 파일을 요청하는

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 

가 어떻게 다음 파이썬에서이 요청을 할 것입니까? 저는 현재 다음과 같이하려고합니다 :

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

그러나 나는 404를 얻고 있습니다. 어떻게이 요청을 할 수 있습니까?

답변

1

You have to authenticate your request. 답에 대한

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

감사 :

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

그런 다음 서비스를 사용 : 워드 프로세서

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

: 나는 상용구를 많이 제거하기 위해 구글의 파이썬 클라이언트를 사용하는 것이 좋습니다 . 이제 나는 그것을 설치했다.'api_key'와'file_id'가 주어지면 어떻게 파일을 다운로드 할 수 있을까요? – David542

+0

감사합니다.'service = build_service (### Credentials here ###) '행에 대한 예제를 제공해 주시겠습니까? – David542

관련 문제