七牛的api fetch 功能 怎么试都是401 错误

有木有好人 给段可以用的代码 实例,是fetch的

#coding:utf8
import hashlib
import hmac
import base64
import requests
import logging
'''
POST /fetch/<EncodedURL>/to/<EncodedEntryURI> HTTP/1.1
Host:           iovip.qbox.me
Content-Type:   application/x-www-form-urlencoded
Authorization:  QBox <AccessToken>
'''

host = "http://iovip.qbox.me"
bucket = "test-nba"
accesskey = "..."
secertkey = "..." 

logger = logging.getLogger("qiniu_fetch")
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
logger.addHandler(ch)

def mk_token(path):
    sign = hmac.new(secertkey, path, hashlib.sha1).digest()
    encodeSign = base64.b64encode(sign)
    return "%s:%s" %(accesskey, encodeSign)

def gen_path(fromurl, key):
    encodedUrl = base64.b64encode(fromurl)
    encodedEntryURI = base64.b64encode(bucket+":"+key)
    return "/fetch/%s/to/%s\n" %( encodedUrl, encodedEntryURI)

def fetch_qiniu(fromurl, key):
    path = gen_path(fromurl, key)
    token = mk_token(path)
    logger.info(token)
    headers = {
        'Host': 'iovip.qbox.me',
        'Content-Type' : 'application/x-www-form-urlencoded',
        'Authorization' : "QBox %s" % token
        }
    r = requests.post(host+path,headers=headers)
    return r

def fetch_qiniu_http(fromurl,key):
    import httplib, urllib
    path = gen_path(fromurl, key)
    token = mk_token(path)
    logger.info(token)
    headers = {
        'Host': 'iovip.qbox.me',
        'Content-Type' : 'application/x-www-form-urlencoded',
        'Authorization' : "QBox %s" % token
        }

    conn = httplib.HTTPConnection("iovip.qbox.me")
    conn.request("POST", path,  headers=headers)
    response = conn.getresponse()
    return response.status,response.read()

if __name__ == "__main__":
    url = "http://i1.hoopchina.com.cn/blogfile/201403/14/BbsImg139477565816182_610*610.jpg"
    key = "123.jpg"

    print fetch_qiniu(url,key)
阅读 4.2k
1 个回答

可以贴下你写的 fetch 代码

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
logo
七牛云问答
子站问答
访问
宣传栏