通过Google Drive API下载Google Drive文件

时间:2019-01-03 01:09:11

标签: google-drive-api

我已经通过python代码进行了服务器帐户身份验证:

APP_CREDENTIALS = service_account.Credentials.from_service_account_file(
    #config.EE_ACCOUNT,
    config.EE_PRIVATE_KEY_FILE,
    scopes = OAUTH_SCOPE)

service = build('drive', 'v3', credentials=APP_CREDENTIALS)

然后我键入以下代码以在驱动器中打印这些文件:

state = task.status()['state']
        if state == ee.batch.Task.State.COMPLETED:
            logging.info('Task succeeded (id: %s).', task.id)

        results = service.files().list().execute()
        items = results.get('files', [])
        if not items:
            print('No files found.')
        else:
            print('Files:')
            for item in items:
                if item['id'] == '1MiXjaHXU3ktOrkVwdkAABuNM-HXhJGGF':
                    print('downloadfiles')
                    download_file(item['id'], service)
                print(u'{0} ({1})'.format(item['name'], item['id']))

结果是:

NDVI_1229.tif (1MiXjaHXU3ktOrkVwdkAABuNM-HXhJGGF)
NDVI_1229.tif (1y5anN0zpRYW180pd0t74xbOWxr0bjfdm)
NDVI_1229.tif (1X8fYAzRrUBxPMLpZmx_bJULLwq09lJPb)
NDVI_1229.tif (1iSk51dCTAyiVjPbCFQ3irnjhYsn708mV)
NDVI_1229.tif (1Cr3G7tZF2xYAmO70n6tvSs65Ot7MWr69)
NDVI_1229.tif (1KlGUVmr3maaiya4WAeD0ShI6DAsUcxT5)
NDVI_1229.tif (1kkGxRQulWYIG7tX0J8f_n5T1hwhYK_O0)
NDVI_1229.tif (1AqgchB8X7aul1rhVk76GlF1Iwm5N-UFR)
NDVI_1229.tif (14A7Kzcweaft8eBof_nWQGtLQ5RwvpFlP)
NDVI.tif (1GvY3JlMmqE-TgvqR5Fg8zPa9JvuJAyHJ)
NDVI.tif (15N4Kge7gR5bk7B3ZwRBG4Tad0PKU9nPc)
NDVI.tif (19cr8Ena_oztorgmOQL-FTBvNq4vN7nD9)
NDVI.tif (1oK5N5GTiTthkpk6rtjFF9wM4qeXDtESR) 

现在,我要下载这些文件,并使用Google Drive API官方网站中提供的方法。代码如下所示:

def download_file(file_id, drive_service):
    request = drive_service.files().get_media(fileId=file_id)
    fh = io.BytesIO()
    downloader = http.MediaIoBaseDownload(fh, request)
    done = False
    while done is False:
        status, done = downloader.next_chunk()
        print "Download %d%%." % int(status.progress() * 100)
    return fh.getvalue()

我得到了这个答案:

INFO     2019-01-02 08:32:04,585 discovery.py:871] URL being requested: GET https://www.googleapis.com/drive/v3/files/1MiXjaHXU3ktOrkVwdkAABuNM-HXhJGGF?alt=media
Download 100%.

我打开了链接,其内容如下:

{
 "error": {
  "errors": [
   {
    "domain": "usageLimits",
    "reason": "dailyLimitExceededUnreg",
    "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.",
    "extendedHelp": "https://code.google.com/apis/console"
   }
  ],
  "code": 403,
  "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."
 }
}

所以我很困惑我的身份验证方法是否正确。 而且,我尝试了不同的方式从该驱动器下载文件或将该驱动器的文件导出到其他用户的驱动器,它具有相同的答案。

0 个答案:

没有答案