• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

python备份文件到Google Drive网盘

python 水墨上仙 2833次浏览

1、要有一个Google App账号:

这个可以上网上去申请,申请地址为:https://developers.google.com/appengine/?hl=zh-cn

2、创建一个Google App应用:

然后注到
https://appengine.google.com/创建一个应用,创建应用时要选择本地应用,scope选择https://www.useso.com/auth/drive

3、创建应用成功以后到
https://code.google.com/apis/console/查看你的应用的信息,点击API Access,将client id,client secret,redirect uri记录下来,

下面就是测试代码

代码转自:
http://blog.csdn.net/karldoenitz

#coding=utf-8
import httplib2
import pprint

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


# Copy your credentials from the APIs Console
CLIENT_ID = ''
CLIENT_SECRET = ''

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

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

# Path to the file to upload
FILENAME = 'document.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 Documents',
  'description': 'A test document',
  'mimeType': 'text/plain'
}

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

运行代码后会给你一个地址,将地址复制到浏览器的地址栏里,加载出页面后点击accept,会给你一个字符串,复制下来,

输入到后台,回车等待后台显示大批代码后表名文件上传成功。

如果有部分人上传不成功,可以使用VPN进行翻墙,剩下的就不多说了。


喜欢 (0)
加载中……