【发布时间】:2014-05-01 06:56:23
【问题描述】:
我尝试了谷歌驱动上传文件的示例代码。我有 2 个谷歌帐户。当我设置账户 A 的 CLIENT_ID 和 CLIENT_SECRET 时,它可以成功上传文件到账户 A。但是,当我设置账户B的CLIENT_ID和CLIENT_SECRET时,它仍然上传文件到账户A。如何设置上传文件到账户B?
代码与谷歌的示例代码相同。我只是替换 CLIENT_ID 和 CLIENT_SECRET 和 REDIRECT_URI
import httplib2
import pprint
from apiclient.discovery import build
from apiclient.http import MediaFileUpload
from oauth2client.client import OAuth2WebServerFlow
CLIENT_ID = 'My account CLIENT_ID'
CLIENT_SECRET = 'My account CLIENT_SECRET'
OAUTH_SCOPE = 'https://www.googleapis.com/auth/drive'
REDIRECT_URI = 'http://lingdisk.synology.me/'
FILENAME = 'Document.txt'
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)
http = httplib2.Http()
http = credentials.authorize(http)
drive_service = build('drive', 'v2', http=http)
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()
【问题讨论】:
-
你能告诉我们你到目前为止的代码吗?
标签: google-drive-api