我一直在使用Postman使用Oauth2,它运行得很好。我按预期得到了令牌。
然而,当我在代码中尝试此操作时,它会生成所有内容,但我从未返回要使用的代码(代码在我的数据库中,但它不会从jquery调用返回)。
function validate(redirectURL) {
console.log('start validate function');
var clientID2 = "CLIENTID HERE";
var redirectURL2 = browser.identity.getRedirectURL();
let m = redirectURL.match(/[#\?](.*)/);
if (!m || m.length < 1)
return null;
let params = new URLSearchParams(m[1].split("#")[0]);
let tokenurl = "http://localhost:8000/oauth/access_token";
// tokenurl += `?client_id=${clientID2}`;
// tokenurl += `?client_secret=${clientSecret}`;
// tokenurl += `&grant_type=authorization_code`;
// tokenurl += `&code=${params.get("code")}`;
// tokenurl += `&redirect_uri=${encodeURIComponent(redirectURL2)}`;
console.log('validate function tokenurl '+ tokenurl);
$.ajax({
method: "POST",
url: tokenurl,
async: true,
crossDomain: true,
headers: {
"content-type": "application/x-www-form-urlencoded",
"cache-control": "no-cache"
},
data : {
client_id : clientID2,
client_secret : "CLIENT ID SECRET",
redirect_uri : redirectURL2,
grant_type : "authorization_code",
code : params.get("code"),
}
})
.done(function( msg ) {
console.log('Done return for validate call');
// console.log( "Token Save Called: " + msg );
})
.fail(function(jqXHR, textStatus, errorThrown){
console.log('Fail return for validate call');
if(textStatus){
console.log('validate call returned textStatus-' + textStatus);
}
if(errorThrown){
console.log('validate call returned errorThrown-'+errorThrown);
}
console.log('Fail return for validate call Finish');
})
.always(function( xhr, status ) {
console.log('finish validate call ajax request');
});
}
function authorize() {
console.log('authorize2');
const redirectURL = browser.identity.getRedirectURL();
console.log('redirectURL');
console.log(encodeURIComponent(redirectURL));
console.log(redirectURL);
const clientID = "CLIENTID HERE";
const scopes = ["openid", "email", "profile"];
let authURL = "http://localhost:8000/oauth/authorize";
authURL += `?client_id=${clientID}`;
authURL += `&grant_type=authorization_code`;
authURL += `&client_secret=4c7f6f8fa93d59c45502c0ae8c4a95b`;
authURL += `&response_type=code`;
authURL += `&redirect_uri=${encodeURIComponent(redirectURL)}`;
// authURL += `&scope=${encodeURIComponent(scopes.join(' '))}`;
console.log('redirectURL');
console.log(redirectURL);
console.log('authURL');
console.log(authURL);
return browser.identity.launchWebAuthFlow({
interactive: true,
url: authURL
});
}
function getAccessToken() {
console.log('call authorize');
return authorize().then(validate);
}
console.log('call getAccessToken');
getAccessToken();
我没有返回任何数据,控制台日志输出错误一词。这些函数被调用并进入我的后端,因为它正在制作并将最终代码存储在数据库中。
我正在使用这个laravel oauth包 https://github.com/lucadegasperi/oauth2-server-laravel版本5.1
我在运行版本54的Firefox Developer Edition上的firefox webextension中使用它,它支持Oauth2调用(基于文档)。