本文介绍了2小时后Google Drive Python API可恢复上传错误401的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 首先,我很抱歉,如果这是一个太愚蠢的问题......这是我第一次尝试使用这个脚本中涉及的任何技术(Python,驱动api,oauth 2.0,等等)。我发誓,在发布问题之前,我一直在寻找并尝试这个工作大约一周。 hehehe 我试图用google-api-python-client上传一个仅在Linux Debian上的大文件(3.5GiB)。我已经成功上传小文件,但是当我尝试上传大文件时,在HTTP 401错误(未经授权)启动后,上传停止大约1〜2小时。我一直在寻找如何获得新的访问令牌,但几乎没有成功。 这是我的(更新的)代码: $ p $ #!/ usr / bin / python 导入httplib2 导入pprint 导入时间 $ b从apiclient.discovery导入构建从apiclient.http导入MediaFileUpload 从apiclient导入错误$ b $从oauth2client.client导入OAuth2WebServerFlow #从API控制台复制凭证 CLIENT_ID ='myclientid' CLIENT_SECRET ='myclientsecret' #查看https://developers.google.com所有可用作用域的驱动器/作用域 OAUTH_SCOPE ='https://www.googleapis.com/auth/drive' #为已安装的应用程序重定向URI REDIRECT_URI =' urn:ietf:wg:oauth:2.0:oob' #运行OAuth流并检索证书 flow = OAuth2WebServerFlow(CLIENT_ID,CLIENT_SECRET,OAUTH_SCOPE,REDIRECT_URI) authorize_url = fl ow.step1_get_authorize_url() print'转到您浏览器中的以下链接:'+ authorize_url code = raw_input('输入验证码:').strip() credentials = flow。 step2_exchange(code) #创建一个httplib2.Http对象并使用我们的凭据授权它 http = httplib2.Http() http = credentials.authorize(http) $ b $ drive_service = build('drive','v2',http = http) #插入文件 media_body = MediaFileUpload('bigfile.zip',mimetype = 'application / octet-stream',chunksize = 1024 * 256,resumable = True) body = {'title':'bigfile.zip','description':'Big File ','mimeType':'application / octet-stream'} retries = 0 request = drive_service.files()。insert(body = body ,media_body = media_body) response = None while response is None: try: print http.request.credentials.access_token status, response = request.next_chunk()如果状态: print已上传%.2f %%%(status.progress()* 100) retries = 0 , .HttpError,e:,如果e.resp.status == 404: printError 404!正在中止。 exit()其他:如果重试> 10: print超出重试限制! exit() else: retries + = 1 time.sleep(2 ** retries) print错误(%d).. %e.resp.status continue print上传完成! 经过一番挖掘后,我发现授权的http对象在接收到401后会自动刷新访问令牌。尽管它实际上正在更改访问令牌,但它仍然没有按预期继续上传......请参阅下面的输出: ya29.AHES6ZTo_-0oDqwn3JnU2uCR2bRjpRGP0CSQSMHGr6KvgEE 已上传2.28% ya29.AHES6ZTo_-0oDqwn3JnU2uCR2bRjpRGP0CSQSMHGr6KvgEE 已上传2.29 % ya29.AHES6ZTo_-0oDqwn3JnU2uCR2bRjpRGP0CSQSMHGr6KvgEE 上传2.29% ya29.AHES6ZTo_-0oDqwn3JnU2uCR2bRjpRGP0CSQSMHGr6KvgEE 上传2.30% ya29.AHES6ZTo_,0oDqwn3JnU2uCR2bRjpR GP0CSQSMHGr6KvgEE 错误(401)...正在重试。 ya29.AHES6ZQqp3_qbWsTk4yVDdHnlwc_7GvPZiFIReDnhIIiHao 错误(401)...正在重试。 ya29.AHES6ZSqx90ZOUKqDEP4AAfWCVgXZYT2vJAiLwKDRu87JOs 错误(401)...正在重试。 ya29.AHES6ZTp0RZ6U5K5UdDom0gq3XHnyVS-2sVU9hILOrG4o3Y 错误(401)...正在重试。 ya29.AHES6ZSR-IOiwJ_p_Dm-OnCanVIVhCZLs7H_pYLMGIap8W0 错误(401)...正在重试。 ya29.AHES6ZRnmM-YIZj4S8gvYBgC1M8oYy4Hv5VlcwRqgnZCOCE 错误(401)...正在重试。 ya29.AHES6ZSF7Q7C3WQYuPAWrxvqbTRsipaVKhv_TfrD_gef1DE 错误(401)...正在重试。 ya29.AHES6ZTsGzwIIprpPhCrqmoS3UkPsRzst5YHqL-zXJmz6Ak 错误(401)...正在重试。 ya29.AHES6ZSS_1ZBiQJvZG_7t5uW3alsy1piGe4-u2YDnwycVrI 错误(401)...正在重试。 ya29.AHES6ZTLFbBS8mSFWQ9zK8cgbX8RPeLghPxkfiKY54hBB-0 错误(401)...正在重试。 ya29.AHES6ZQBeMWY50z6fWXvaCcd5_AJr_AYOuL2aiNKpK-mmyU 错误(401)...正在重试。 ya29.AHES6ZTs2mYYSEyOqI_Ms4itKDx36t39Oc5RNZHkV4Dq49c 超出限制!中止。 我使用安装了Python 2.5.2的debian lenny,并安装了ssl和google-api -python-client通过大约一周前的pip安装。 感谢您提供任何帮助。 strong>编辑:显然,问题不在于api。我尝试了上面的相同代码,但有两个小文件,它们之间有1h(system.sleep())。输出结果为: ya29.AHES6ZRUssiLfuhqCP9Cu7C7LuhRV2rYzPldU27wiMJZWb8 已上传66.89% ya29.AHES6ZRUssiLfuhqCP9Cu7C7LuhRV2rYzPldU27wiMJZWb8 上传1完成! ya29.AHES6ZRUssiLfuhqCP9Cu7C7LuhRV2rYzPldU27wiMJZWb8 已上传57.62% ya29.AHES6ZQd3o1ciwXpNFImH3CK0-dJAtQba_oeIO9DDbIq154 上传2完成! 对于第二次上传,成功使用了新的访问令牌。所以,也许可恢复的会话在一段时间后过期,或者只对那个特定的访问令牌有效? 我提交了一个问题,并根据来自谷歌的Joe Gregorio,问题出现在后端: 这是后端问题,而不是API或与您的正如你推断的那样,如果上传时间太长,access_token会过期,那么可恢复的上传将无法继续。目前正在解决此问题的工作正在进行中,一旦问题得到解决,我将更新此错误在服务器端。 First of all, I'm sorry if this is a too silly question... this is the first time I'm trying to use any of the technologies involved in this script (Python, the drive api, oauth 2.0, etc). I swear I've been searching and trying this for about a week before posting the question. heheheI'm trying to use the google-api-python-client to upload a big file (3.5GiB) that is on a terminal only Linux Debian. I've had some success uploading small files, but when I try to upload the big file, the upload stops about 1~2 hours after it started with HTTP 401 error (unauthorized). I've been looking on how to get a new access token but have had little success.This is my (updated) code so far:#!/usr/bin/pythonimport httplib2import pprintimport timefrom apiclient.discovery import buildfrom apiclient.http import MediaFileUploadfrom apiclient import errorsfrom oauth2client.client import OAuth2WebServerFlow# Copy your credentials from the APIs ConsoleCLIENT_ID = 'myclientid'CLIENT_SECRET = 'myclientsecret'# Check https://developers.google.com/drive/scopes for all available scopesOAUTH_SCOPE = 'https://www.googleapis.com/auth/drive'# Redirect URI for installed appsREDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'# Run through the OAuth flow and retrieve credentialsflow = 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_urlcode = raw_input('Enter verification code: ').strip()credentials = flow.step2_exchange(code)# Create an httplib2.Http object and authorize it with our credentialshttp = httplib2.Http()http = credentials.authorize(http)drive_service = build('drive', 'v2', http=http)# Insert a filemedia_body = MediaFileUpload('bigfile.zip', mimetype='application/octet-stream', chunksize=1024*256, resumable=True)body = { 'title': 'bigfile.zip', 'description': 'Big File', 'mimeType': 'application/octet-stream'}retries = 0request = drive_service.files().insert(body=body, media_body=media_body)response = Nonewhile response is None: try: print http.request.credentials.access_token status, response = request.next_chunk() if status: print "Uploaded %.2f%%" % (status.progress() * 100) retries = 0 except errors.HttpError, e: if e.resp.status == 404: print "Error 404! Aborting." exit() else: if retries > 10: print "Retries limit exceeded! Aborting." exit() else: retries += 1 time.sleep(2**retries) print "Error (%d)... retrying." % e.resp.status continueprint "Upload Complete!"After some digging, I found out that the authorized http object automatically refreshes the access token after receiving 401. Although it's really changing the access token, it's still not continuing the upload as expected... see the output below:ya29.AHES6ZTo_-0oDqwn3JnU2uCR2bRjpRGP0CSQSMHGr6KvgEEUploaded 2.28%ya29.AHES6ZTo_-0oDqwn3JnU2uCR2bRjpRGP0CSQSMHGr6KvgEEUploaded 2.29%ya29.AHES6ZTo_-0oDqwn3JnU2uCR2bRjpRGP0CSQSMHGr6KvgEEUploaded 2.29%ya29.AHES6ZTo_-0oDqwn3JnU2uCR2bRjpRGP0CSQSMHGr6KvgEEUploaded 2.30%ya29.AHES6ZTo_-0oDqwn3JnU2uCR2bRjpRGP0CSQSMHGr6KvgEEError (401)... retrying.ya29.AHES6ZQqp3_qbWsTk4yVDdHnlwc_7GvPZiFIReDnhIIiHaoError (401)... retrying.ya29.AHES6ZSqx90ZOUKqDEP4AAfWCVgXZYT2vJAiLwKDRu87JOsError (401)... retrying.ya29.AHES6ZTp0RZ6U5K5UdDom0gq3XHnyVS-2sVU9hILOrG4o3YError (401)... retrying.ya29.AHES6ZSR-IOiwJ_p_Dm-OnCanVIVhCZLs7H_pYLMGIap8W0Error (401)... retrying.ya29.AHES6ZRnmM-YIZj4S8gvYBgC1M8oYy4Hv5VlcwRqgnZCOCEError (401)... retrying.ya29.AHES6ZSF7Q7C3WQYuPAWrxvqbTRsipaVKhv_TfrD_gef1DEError (401)... retrying.ya29.AHES6ZTsGzwIIprpPhCrqmoS3UkPsRzst5YHqL-zXJmz6AkError (401)... retrying.ya29.AHES6ZSS_1ZBiQJvZG_7t5uW3alsy1piGe4-u2YDnwycVrIError (401)... retrying.ya29.AHES6ZTLFbBS8mSFWQ9zK8cgbX8RPeLghPxkfiKY54hBB-0Error (401)... retrying.ya29.AHES6ZQBeMWY50z6fWXvaCcd5_AJr_AYOuL2aiNKpK-mmyUError (401)... retrying.ya29.AHES6ZTs2mYYSEyOqI_Ms4itKDx36t39Oc5RNZHkV4Dq49cRetries limit exceeded! Aborting.I'm using debian lenny with Python 2.5.2 installed, and installed the ssl and google-api-python-client through pip install about a week ago.Thanks in advance for any help.EDIT: Apparently, the problem isn't with the api. I tried the same code above, but with two small files, with 1h between them (system.sleep()). The output was:ya29.AHES6ZRUssiLfuhqCP9Cu7C7LuhRV2rYzPldU27wiMJZWb8Uploaded 66.89%ya29.AHES6ZRUssiLfuhqCP9Cu7C7LuhRV2rYzPldU27wiMJZWb8Upload 1 Complete!ya29.AHES6ZRUssiLfuhqCP9Cu7C7LuhRV2rYzPldU27wiMJZWb8Uploaded 57.62%ya29.AHES6ZQd3o1ciwXpNFImH3CK0-dJAtQba_oeIO9DDbIq154Upload 2 Complete!For the second upload, a new access token was used successfully. So, perhaps the resumable session is expiring after some time or is only valid for that specific access token? 解决方案 I filed an issue on the google-api-python-client project, and according to Joe Gregorio from google, the problem is in the backend:"This is an issue with the backend and not with the API or with your code. As you deduced, if the upload goes too long the access_token expires and at that point the resumable upload can't be continued. There is work on progress to fix this issue right now, I will update this bug once the issue is fixed on the server side." 这篇关于2小时后Google Drive Python API可恢复上传错误401的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-20 16:23