我正在使用下面的Python脚本将文件上传到Google云端硬盘。当需要用户交互来获取授权密钥时,它首次上传正常。但是,当我尝试再次上传文件时,会出现错误:
引发HttpError(resp,content,uri = self.uri) apiclient.errors.HttpError:https://www.googleapis.com/upload/drive/v2/files?uploadType=resumable&alt=json返回"无效的上传请求">
这到底是什么意思? 我猜测刷新令牌并不完全符合我的要求。
#!/usr/bin/python
import httplib2
import pprint
from apiclient.discovery import build
from apiclient.http import MediaFileUpload
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.file import Storage
CLIENT_ID = '*************************************************************************'
CLIENT_SECRET = '*****************************'
OAUTH_SCOPE = 'https://www.googleapis.com/auth/drive'
REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'
FILENAME = '/home/student/Desktop/TraceRoute.kml'
CRED_FILENAME = 'credentials'
### For storing token
storage = Storage(CRED_FILENAME)
if not storage.get():
# Run through the OAuth flow and retrieve authorization code
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)
### Storing access token and a refresh token in CRED_FILENAME
storage.put(credentials)
else:
### Getting access_token,expires_in,token_type,Refresh_toke info from CRED_FILENAME to 'credentials'
credentials = storage.get()
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)