使用 JavaScript 的 DeviantArt Oauth 身份验证
DeviantArt Oauth authentication with JavaScript
我正在尝试通过 JavaScript 获取 Oauth 访问令牌。在 partilar 我想验证 DeviantArt API 保持尽可能简单。这是我的方法,我只需要一个,但任何人都在工作,所以只要一个解决方案就足够了。
因为它是 Cross-Origin 我尝试使用 ajax。这是代码:
var response_type = "code";
var client_id = "1234"; //Random clientId for Whosebug
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 Whosebug
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() 中的 headers 或向 getJson 添加选项,但无论我尝试什么,总是出现 CORS 错误。
我一直在尝试在 Whosebug 上发布的数十种解决方案,但没有一个适合我:(
我在尝试获取简单的客户端访问令牌时遇到了类似的问题,我得出的结论是 Deviantart API 根本不支持 CORS。我不是 100% 确定,但我的尝试(通过 ajax 或 XMLHttpRequest 甚至 test-cors.org)没有任何迹象表明响应包含正确的 CORS header (Access-Control-Allow-Origin).
这也有道理。这种使用 API 的方式会强制您在 JS 代码中公开您的客户端密码(正如您在示例中所做的那样)。我相信 API 旨在用于 back-end 或程序(桌面或移动),这些程序将在其代码或某些加密的配置文件中隐藏其客户端机密。 This comment 在另一个问题上让我得出了那个结论。
如果可能,我建议您处理 API 通信 server-side。您可能还想看看 their oEmbed implementation.
我正在尝试通过 JavaScript 获取 Oauth 访问令牌。在 partilar 我想验证 DeviantArt API 保持尽可能简单。这是我的方法,我只需要一个,但任何人都在工作,所以只要一个解决方案就足够了。
因为它是 Cross-Origin 我尝试使用 ajax。这是代码:
var response_type = "code"; var client_id = "1234"; //Random clientId for Whosebug 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 Whosebug 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() 中的 headers 或向 getJson 添加选项,但无论我尝试什么,总是出现 CORS 错误。
我一直在尝试在 Whosebug 上发布的数十种解决方案,但没有一个适合我:(
我在尝试获取简单的客户端访问令牌时遇到了类似的问题,我得出的结论是 Deviantart API 根本不支持 CORS。我不是 100% 确定,但我的尝试(通过 ajax 或 XMLHttpRequest 甚至 test-cors.org)没有任何迹象表明响应包含正确的 CORS header (Access-Control-Allow-Origin).
这也有道理。这种使用 API 的方式会强制您在 JS 代码中公开您的客户端密码(正如您在示例中所做的那样)。我相信 API 旨在用于 back-end 或程序(桌面或移动),这些程序将在其代码或某些加密的配置文件中隐藏其客户端机密。 This comment 在另一个问题上让我得出了那个结论。
如果可能,我建议您处理 API 通信 server-side。您可能还想看看 their oEmbed implementation.