Den*_*isL 5 javascript api json oauth angularjs
目前,我正在开发一个需要我连接到外部 API 以获取 JSON 文件的 Web 应用程序。
我正在使用需要 Oauth1.0a 身份验证的名词项目的相关 API 。现在这个项目需要我使用 Angular.JS 来处理 JSON 数据。
但在我可以使用 JSON 之前,我需要获取它,这就是事情分崩离析的地方。当我尝试使用以下代码连接时,我的http://localhost:8080/上不断出现以下错误。
错误 :
> XMLHttpRequest cannot load
> http://api.thenounproject.com/icons/fish&callback=?&oauth_consumer_key=9c70…891xxxx&oauth_version=1.0&oauth_signature=xxxxx6oeQI0p5U%2Br0xxxxxxx%3D.
> No 'Access-Control-Allow-Origin' header is present on the requested
> resource. Origin 'http://localhost:8080' is therefore not allowed
> access. The response had HTTP status code 403.
> Blockquote
Run Code Online (Sandbox Code Playgroud)
编码 :
var oAuth = OAuth({
consumer: {
public: '9c704cb01xxxxxxxxx',
secret: '45b7a8d86xxxxxxxxx'
},
signature_method: 'HMAC-SHA1'
});
var app = angular.module('nounProject', []);
app.controller('apiController', function(){
console.log("check");
var request_data = {
url: 'http://api.thenounproject.com/icons/fish&callback=?',
method: 'GET'
};
// var token = {
// public: 'f5fa91bedfd5xxxxxxxxxx',
// secret: '84228963d5e8xxxxxxxxxx'
// };
$.ajax({
url: request_data.url,
type: request_data.method,
data: oAuth.authorize(request_data)
}).done(function(data) {
console.log(data);
});
});
Run Code Online (Sandbox Code Playgroud)
我用来在 JavaScript 中访问 OAuth 的库如下:https : //github.com/ddo/oauth-1.0a#client-side-usage-caution(由 DDO)
任何人都可以指导我朝着正确的方向前进,或者有更好的方式将 OAuth 连接到带有 Angular.JS 的 API?
提前致谢!
正确的方法是客户端 <-> 服务器 <-> oauth 服务
所有 oauth 步骤都应该在您的服务器端。
为什么?简单的答案是您无法在客户端隐藏您的秘密消费者/令牌。
| 归档时间: |
|
| 查看次数: |
10244 次 |
| 最近记录: |