Google app engine Google驱动器在python中读取部分文件

Google app engine Google驱动器在python中读取部分文件,google-app-engine,google-drive-api,Google App Engine,Google Drive Api,我有一个应用引擎站点,我想从Google Drive中的一个文件中获取最后300个字节。我尝试将HTTP范围头与Python的urllib2结合使用,如下所示: req = urllib2.Request(url) req.headers['Range'] = 'bytes=%s-%s' % (-300, 2) f = urllib2.urlopen(req) # This shows you the *actual* bytes that have been downloaded. range

我有一个应用引擎站点,我想从Google Drive中的一个文件中获取最后300个字节。我尝试将HTTP范围头与Python的urllib2结合使用,如下所示:

req = urllib2.Request(url)
req.headers['Range'] = 'bytes=%s-%s' % (-300, 2)
f = urllib2.urlopen(req)
# This shows you the *actual* bytes that have been downloaded.
range=f.headers.get('Content-Range')
logging.info("range:"+range)
logging.info("read:"+(f.read()))
url实际上是文件元数据中的“downloadurl”属性。我在运行它时遇到此错误

  File "C:\Python27\lib\urllib2.py", line 527, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 401: Unauthorized
是否可能不接受范围标头?我真的不想下载整个文件。有人有解决方案吗

编辑

感谢@Claudio Cherubino的回答

creds = GetSessionCredentials() //from Google Python Client API
size = int('size of my file')
headers = {"Range" : 'bytes=%s-%s' % (size-300, size)} //Range Header
http = httplib2.Http()
http = creds.authorize(http) //add OAuth credentials
resp, content = http.request(url, "GET", body=None, headers=headers)
if resp.status == 206:
   print 'Status: %s' % resp
   print "content:"+content
else:
    print 'An error occurred: %s' % resp

您必须向请求中添加OAuth令牌,如文档中所述: