Howto download file from Drive API using Python script -


i'm working on simple script in python download , export files google apps domain drive service. able create drive session using service account , i'm getting json output list query. defined download function according article:

https://developers.google.com/drive/manage-downloads

the problem function return json output called content, can't figure out how store file locally on hdd. looking curl if can used inside python script , found urllib/urllib2 should used curl. if try use urllib2 read remote file by:

remote_file = urllib2.urlopen(download_url).read() 

i 401 error unathorized.

so looks urllib2 working doesn't use stored credentials.

so how can create authorized query using urllib/2? or right way store file locally within script? there other other python or google specific library can me store files locally?

thanks in advance.

edit: using google apis client library. problem function download_file returning json output i'm not able save file local storage.

i tried this:

def download_file(service, drive_file):     """download file's content.      args:             service: drive api service instance.             drive_file: drive file instance.      returns:             file's content if successful, none otherwise.     """     download_url = drive_file.get('downloadurl')     if download_url:             resp, content = service._http.request(download_url)             if resp.status == 200:                     print 'status: %s' % resp                     #return content                     title = drive_file.get('title')                     path = './data/'+title                     file = open(path, 'wb')                #    remote_file = urllib2.urlopen(download_url).authorize().read()                     file.write(content.read())             else:                     print 'an error occurred: %s' % resp                     return none     else:             # file doesn't have content stored on drive.             return none 

this creates file on hdd, fails when tries read content. don't know how process content suitable writing local disk.

edit2:

ok, figure out. mistake tried use function read() on content. have use file.write(content).

you may try script the article. , remember use google apis client library python.

from apiclient import errors # ...  def download_file(service, drive_file):     """download file's content.      args:     service: drive api service instance.     drive_file: drive file instance.      returns:     file's content if successful, none otherwise.     """     download_url = drive_file.get('downloadurl')     if download_url:         resp, content = service._http.request(download_url)     if resp.status == 200:         print 'status: %s' % resp         return content     else:         print 'an error occurred: %s' % resp         return none     else:     # file doesn't have content stored on drive.     return none 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -