【发布时间】:2015-05-05 16:33:35
【问题描述】:
我正在尝试通过 JavaScript 获取 Oauth 访问令牌。特别是,我想对DeviantArt API 进行身份验证,尽可能简单。这是我的方法,我只需要一个,但任何人都在工作,所以只要一个解决方案就足够了。
-
由于它是跨域的,我尝试使用 ajax。这是代码:
var response_type = "code"; var client_id = "1234"; //Random clientId for stackoverflow var redirect_uri = "http://localhost:8080/my-project/authDeviantArt.html"; var authorization1 = "https://www.deviantart.com/oauth2/authorize"; var authorization2 = "https://www.deviantart.com/oauth2/token"; var client_secret = "qwerty12345"; //Random secret for stackoverflow var grant_type1 = "authorization_code"; var grant_type2 = "client_credentials"; var scopes = "basic"; //Using The Authorization Code Grant var request1 = authorization1+'?response_type='+response_type+'client_id='+client_id+'&redirect_uri='+redirect_uri; //Using The Client Credentials Grant var request2 = authorization2+'?client_id='+client_id+'&client_secret='+client_secret+'&grant_type='+grant_type2; $.ajax({ url: request1, dataType: "jsonp", success: function (res){ console.log("Response: "+res.access_token); });
问题是我总是得到以下错误
Uncaught SyntaxError: Unexpected token :
但是点击 JavasCript 控制台中的错误我可以看到响应 JSON:
{"access_token":"662b..5d58","token_type":"Bearer","expires_in":3600,"status":"success"}
Concole.log 打印 Error: [object Object](尽管 chrome 控制台不会在该行抛出任何错误)。
我也尝试使用库 JSO,但我也没有保存令牌。
我也尝试使用库 Hello.js,但它不支持 DeviantArt,我修改它的方法也没有任何成功。
我还尝试修改 XMLHttpRequest() 中的标头或向 getJson 添加选项,但无论我尝试什么,总是出现 CORS 错误。
我一直在尝试在 stackoverflow 上发布的数十种解决方案,但没有一个适合我:(
【问题讨论】:
标签: javascript ajax authentication oauth oauth-2.0