Javascript 具有Oauth2 granttype密码的主干
我想用授权类型密码调用OAuth2服务。Javascript 具有Oauth2 granttype密码的主干,javascript,jquery,ajax,backbone.js,oauth-2.0,Javascript,Jquery,Ajax,Backbone.js,Oauth 2.0,我想用授权类型密码调用OAuth2服务。 我在Jquery中使用主干 帖子的参数: 授权类型=密码 客户端id=[您的应用程序id] 客户机密=[您的客户机密] 用户名=[用户名] 密码=[用户密码] 我尝试了几个NPM插件,但都出现了错误 我已经创建了一个自定义的post AJAX,但这也不起作用: var results = $.ajax({ // The URL to process the request url : "https://ap
我在Jquery中使用主干 帖子的参数:
授权类型=密码
客户端id=[您的应用程序id]
客户机密=[您的客户机密]
用户名=[用户名]
密码=[用户密码]
我尝试了几个NPM插件,但都出现了错误 我已经创建了一个自定义的post AJAX,但这也不起作用:
var results = $.ajax({
// The URL to process the request
url : "https://app1pub.smappee.net/dev/v1/oauth2/token",
type : "POST",
data : {
grant_type : "password",//jshint ignore:line
username: "myuser",
password: "secret",
client_id: "myclient",//jshint ignore:line
client_secret: "clientSecret"//jshint ignore:line
},
beforeSend: function (xhr) {
xhr.setRequestHeader("Authorization", "Bearer $token");
xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
},
dataType: "json",
contentType: "application/x-www-form-urlencoded",
success: function(response) {
//console.log(response);
console.log(response.access_token);//jshint ignore:line
data.access_token = response.access_token;//jshint ignore:line
//tokenGranted();
}
});
return results.responseText;
(见小提琴:)请告诉我错误:
"XMLHttpRequest cannot load https://app1pub.smappee.net/dev/v1/oauth2/token. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9000' is therefore not allowed access."
有人知道一个NPM插件可以使用密码granttype吗?最好举个例子。我试过几次,但都没用
或者一个AJAX调用,它可以工作吗?或者我做错了什么?试试这个,但我仍然有一个“访问控制允许来源”错误的问题
var url = 'https://app1pub.smappee.net/dev/v1/oauth2/token';
// ajax call
$.ajax({
type: "POST",
url: url,
data: {"grant_type": "password", "client_id": client_id, "client_secret": client_secret,"username": username,"password": password},
beforeSend : function(xhr) {
xhr.setRequestHeader("POST", "/dev/v1/oauth2/token HTTP/1.1");
xhr.setRequestHeader("HOST", "app1pub.smappee.net");
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
},
error : function() {
// error handler
},
success: function(data) {
// success handler
alert(data["access_token"])
}