y***@163.com
y***@163.com
  • 发布:2017-05-09 13:15
  • 更新:2017-05-11 19:42
  • 阅读:1801

mui+js写七牛云fetch请求授权认证失败,不知是ajax的head请求写错了还是path拼接错误。

分类:HTML5+

ajax请求总是异常处理返回“Unauthorized”,请问mui.ajax的headers是怎么写的。

//获取管理凭证
function genManageToken(accessKey, secretKey, pathAndQuery, body) {
var str = pathAndQuery + "\n" + body;
var hash = CryptoJS.HmacSHA1(str, secretKey);
var encodedSign = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(hash));
return accessKey + ":" + encodedSign;
}
var fetchImg = function(picUrl) {
// 通过fetch进行远程图片抓取
var accessKey = "AK";
var secretKey = "SK";
//网络图片picurl上传到七牛
var srcUrl = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(picUrl));
var bucket = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse("bucket"));
var path = "/fetch/" + srcUrl + "/to/" + bucket;
var fetchUrl = "http://iovip.qbox.me/" + path;
mui.ajax(fetchUrl, {
dataType: 'json', //服务器返回json格式数据
type: 'post', //HTTP请求类型
timeout: 10000, //超时时间设置为10秒;
headers: {
'Authorization': "QBox " + genManageToken(accessKey, secretKey, path, ""),
'Content-Type': 'application/json',
},
success: function(data) {
//服务器返回响应,根据响应结果,分析是否登录成功;
data = JSON.stringify(data);
data = eval("(" + data + ")");
//输出响应成功key值
console.log(data['key']);
},
error: function(xhr, type, errorThrown) {
//异常处理;
console.log(errorThrown);
}
});
}

2017-05-09 13:15 负责人:无 分享
已邀请:
y***@163.com

y***@163.com (作者)


function safe64(base64) {  
    base64 = base64.replace(/\+/g, "-");  
    base64 = base64.replace(/\//g, "_");  
    return base64;  
}  
function genManageToken(accessKey, secretKey, pathAndQuery, body) {  
    var str = pathAndQuery + "\n" + body;  
    var hash = CryptoJS.HmacSHA1(str, secretKey);     
    var encodedSign = safe64(hash.toString(CryptoJS.enc.Base64));  
    return accessKey + ":" + encodedSign;  
}
```编码问题解决了

该问题目前已经被锁定, 无法添加新回复