我尝试了google驱动器上传文件的示例代码。我有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()
答案 0 :(得分:0)
这是一个愚蠢的错误。我保存凭证并使用旧凭证,即使我在代码中更改了新的CLIENT_ID和CLIENT_SECRET以及REDIRECT_URI。