每5秒钟使用python将文本文件上传到Google驱动器

用户3991421

我如何修改以下python代码,以每5秒钟将ecg.txt上传到Google驱动器,以更新此文本文件。现在,代码每次上传文件时都要求Oauth,我希望它仅在第一次时要求身份验证。

#!/usr/bin/python

import httplib2
import pprint

from apiclient.discovery import build
from apiclient.http import MediaFileUpload
from oauth2client.client import OAuth2WebServerFlow


# Copy your credentials from the console
CLIENT_ID = XXXXXX
CLIENT_SECRET = XXXXX

# Check https://developers.google.com/drive/scopes for all available scopes
OAUTH_SCOPE = 'https://www.googleapis.com/auth/drive'

# Redirect URI for installed apps
REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'

# Path to the file to upload
FILENAME = 'ecg.txt'

# Run through the OAuth flow and retrieve credentials
flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, OAUTH_SCOPE, REDIRECT_URI)
authorize_url = flow.step1_get_authorize_url()
print 'Go to the following link in your browser: ' + authorize_url
code = raw_input('Enter verification code: ').strip()
credentials = flow.step2_exchange(code)

# Create an httplib2.Http object and authorize it with our credentials
http = httplib2.Http()
http = credentials.authorize(http)

drive_service = build('drive', 'v2', http=http)

# Insert a file
media_body = MediaFileUpload(FILENAME, mimetype='text/plain', resumable=True)
body = {
  'title': 'My document',
  'description': 'A test document',
  'mimeType': 'text/plain'
}

file = drive_service.files().insert(body=body, media_body=media_body).execute()
pprint.pprint(file)
瑞安

如果您使用装饰器(如视频波纹所示),它将为您将访问令牌存储在db中,并在需要时处理刷新。

https://www.youtube.com/watch?v=HoUdWBzUZ-M

https://developers.google.com/api-client-library/python/guide/google_app_engine

from oauth2client.appengine import OAuth2Decorator

decorator = OAuth2Decorator(client_id=CLIENT_ID, 
                            client_secret=CLIENT_SECRET,
                            scope=OAUTH_SCOPE,
                            callback_path=REDIRECT_URI)


class MainPage(webapp2.RequestHandler):

    @decorator.oauth_required #Simply place this above any function that requires login.
    def get(self):

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Google Apps脚本将文件上传到我的google驱动器(在GOOGLE中无格式)

使用Android将文本文件上传到Google云端硬盘

在Google驱动器上创建一个文件夹(如果不存在),然后使用Python脚本将文件上传到该文件夹

从Java将本地文件上传到Google驱动器

通过PHP FTP PUT将文本文件上传到Google上传文件

使用curl将zip文件上传到Google驱动器

如何使用gapi和可恢复的上传将文件上传到Google驱动器?

使用google-colaboratory上传到google驱动器的文件的路径是什么?

使用Xamarin.Android将文件上传到Google驱动器文件夹

如何使用使用Python的驱动器API将文件上传到Google驱动器?

如何通过REST API将文件上传到我自己的Google驱动器

如何使用multipart使用Postman将文件上传到Google驱动器?

如何使用react和node将字符串作为文件上传到Google驱动器

如何使用python上传到Google驱动器

使用Google Apps脚本将文件从Google驱动器上传到外部API

将文本附加到我的Google驱动器上的文本文件(Python)

硬盘驱动器以5秒钟的脉冲工作?

通过php脚本将文件匿名上传到google驱动器

运行示例代码以将文件上传到Google驱动器时出错

使用Delphi XE6使用Baas将文本文件上传到云

无法将文件上传到Google驱动器-使用C#

自动将文本文件上传到FTP站点

使用Python api-client-library将文本文件上传到Google Cloud Storage。适用于图片,而非文字

使用gdrive将文件上传到google驱动器在crontab上不起作用

Android将XML文件上传到Google驱动器或Dropbox

无法将多个文件上传到驱动器

如何使用 Google Drive API V3 将文件上传到 Group Drive(共享驱动器)?

将文本文件字典上传到 python 字典不起作用

在 Python 中使用 aiohttp 上传 Google 驱动器文件