![](/img/trans.png)
[英]Firebase: How to generate access token from username and password on the server?
[英]how to get access token from bitly using password and username?
$.ajax({
url: 'https://api-ssl.bitly.com/oauth/access_token',
type: 'POST',
contentType: 'application/x-www-form-urlencoded',
dataType: 'json',
data: { Authorization: "Basic " + btoa('myusername' + ":" + 'mypassword@123') },
success: function (result) {
console.log(result);
},
error: function () {
alert("Cannot get data");
}
});
我正在嘗試通過提供用戶名和密碼從位api獲取訪問令牌,但它顯示invalid_client_authorization錯誤。 有誰有相同的想法嗎?
詳細文檔: http ://dev.bitly.com/authentication.html#resource_owner_credentials
您正在將用戶名與授權標頭串聯在一起。
您的授權和內容類型應放在標頭對象中。
jquery ajax方法上沒有'type'屬性,您可能是說'method'。
此外,您不能同時發送dataType:'json'和將content-type設置為'application / x-www-form-urlencoded'
$.ajax({
url: 'https://api-ssl.bitly.com/oauth/access_token',
methdo: 'POST',
contentType: '',
dataType: 'json',
headers: {
'Authorization' : 'Basic ' + [INSERT YOUR HASH HERE],
'Content-Type' : 'application/x-www-form-urlencoded',
}
data: { $.serialize({
username: YOURUSERNAME,
password: YOURPASSWORD
})},
success: function (result) {
console.log(result);
},
error: function () {
alert("Cannot get data");
}
});
這應該做
固定@goncalomarques答案:
$.ajax({ url: 'https://api-ssl.bitly.com/oauth/access_token', method: 'POST', headers: { 'Authorization': 'Basic ' + btoa(YOURUSERNAME + ':' + YOURPASSWORD), 'Content-Type': 'application/x-www-form-urlencoded', }, success: function(result) { console.log("success"); console.log(result); }, error: function(response) { console.log("Cannot get data"); console.log(response); } });
注意:我可以運行此示例,但是由於跨源異常,無法使用內置的Stackoverflow編輯器(“運行代碼段”)運行它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.