如何将我的驱动器api下载文件用作我的应用引擎应用程序

时间:2016-09-12 06:30:05

标签: python google-app-engine google-drive-api

我使用驱动器api下载pdf文件,我想使用app engine mail api(python)将该文件用作附件。我在下面尝试了以下代码,下载了文件并发送了电子邮件,但在电子邮件中根本没有附件。请帮帮我

class ManageGoogleDriveAPI(webapp2.RequestHandler):
def get(self):

    credentials = AppAssertionCredentials('https://www.googleapis.com/auth/drive')
    http_auth = credentials.authorize(Http())


    # Create a communication between my app and google api endpoint
    DRIVE = build('drive', 'v3', http=http_auth)

    file_id = '18mHxcum4n_-hxDBvmyEZaWTkirPuaWXptYAJG3PWGvU'

    request = DRIVE.files().export_media(fileId=file_id, mimeType='application/pdf')

    fh = io.BytesIO()
    downloader = MediaIoBaseDownload(fh, request)

    done = False
    while done is False:
        status, done = downloader.next_chunk()
        logging.info("Download %d%%." % int(status.progress() * 100))

    filename = 'hello.pdf'

    mail.send_mail(sender='app_name@appspot.gserviceaccount.com'.format(
        app_identity.get_application_id()),
                   to="johndoe@gmail.com",
                   subject=subject,
                   body=body,
                   attachments=[(filename, fh.read())])

    logging.info('COMPLETED!!')
    self.response.write('PROCESS COMPLETED!!, You can close this window now. Thank you!')

1 个答案:

答案 0 :(得分:0)

我终于想到发生了什么......

而不是:

attachments=[(filename, fh.read())])

这样做:

attachments=[(filename, fh.getvalue())])