什么是一个兵API请求阿贾克斯授权头? [英] What are the Ajax authorization headers for a Bing API request?
问题描述
我发送一个Ajax请求到Bing搜索API。当我把它在浏览器中我使用请求的URL的作品。与阿贾克斯我得到一个401错误您所提供的授权类型不被支持。只有基本和OAuth都支持
I'm sending an ajax request to the Bing Search API. The URL I am using for the request works when I put it in the browser. With ajax I get a 401 error "The authorization type you provided is not supported. Only Basic and OAuth are supported"
所以我的头是错误的。它可以在浏览器中,因为我手动输入我的蔚蓝账号密码。
therefore my header is wrong. It works in the browser because I manually type in my azure account key.
<script>
$scope.bingsearch = function() {
var azurekey = '....vjrQiHPp4ct1meqroX2pzQZhPvE';
var keywords = $scope.inputvalue;
var myurl = 'https://api.datamarket.azure.com/Bing/Search/v1/Composite?
Sources=%27web%27&$format=json&Query=%27'+ keywords + '%27';
$http({
method: 'POST',
url: myurl,
headers:{
'Authorization': 'Basic ' + azurekey
}
}).success(function(data){
var searchresults = angular.fromJson(+data);
$scope.searchresult = searchresults;
})
};
</script>
该网址<$c$c>https://api.datamarket.azure.com/Bing/Search/v1/Composite?Sources=%27web%27&$format=json&Query=%27van%20gogh%27$c$c>工作在浏览器中。
如何设置我的头,这样它会接受我的账号密码?
How do I set my header so that it will accept my account key?
推荐答案
我想通了。我不得不为Base64 EN code的头我的Azure键, $ P $一个冒号pfixed 的我去的本网站
贴一个冒号:再加上我的Azure账号密码和base64'd它。
I figured it out. I had to Base64 encode my Azure key in the headers, prefixed by a colon. I went to this website and pasted a colon ":" plus my Azure account key and base64'd it.
所以,我的钥匙 OQA / CS
变成:OQA / CS
然后base64'd到 Ok9RQS9jcw ==
和最终的头部看起来像
So my key OQA/cs
becomes :OQA/cs
then base64'd into Ok9RQS9jcw==
and the final header looks like
`headers: {
'Authorization': 'Basic Ok9RQS9jcw=='
}`
和兵返回我想要的JSON。不要忘记你之前,包括冒号作为的第一个字符的帐户键的的EN code将其的,让你的Base64 EN code为你的关键的一部分的。
and Bing returns the json I want. Do not forget to include the colon as the first character of your account key before you encode it so that you Base64 encode it as part of your key.
这篇关于什么是一个兵API请求阿贾克斯授权头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!