使用Oauth使用Javascript访问Api
Accessing Api with Javascript using Oauth
使用简单的javascript客户端创建访问令牌后,从我的api访问数据时出现问题。
这是我的Js应用程序对象,正如你所看到的,我从我的api返回了一个新的access_token——到目前为止,这一切都很好。我将access_token存储到应用程序中。AccessToken供我在整个应用程序中进行的任何其他api调用中使用。但出于某种原因,当我请求任何东西时,响应总是登录页面,所以基本上,当我尝试访问任何东西时都会被重定向,即使我传递了一个工作日期access_token。
var app = (function(){
/**
* Api
* @type Object
*/
var api = {
AccessToken : null,
views: {},
models: {},
collections: {},
content: null,
router: null,
documents: null,
init: function() {
this.content = $("#content");
this.documents = new api.collections.Documents();
Backbone.history.start();
return this;
},
changeContent: function(el) {
this.content.empty().append(el);
return this;
},
title: function(str) {
// set page title
}
};
/**
* ViewFactory
* @type Object
*/
var ViewFactory = {
documents: function() {
this.documentsView = new api.views.documents({
model: api.documents
});
return this.documentsView;
}
};
/**
* AppRouter
* @type Object
*/
var Router = Backbone.Router.extend({
routes: {
'' : 'documents'
},
documents: function() {
var view = ViewFactory.documents();
api.changeContent(view.$el);
view.render();
}
});
/**
* OAuth
* @type Object
* @return string
*/
var OAuth = {
title : 'Js Client',
clientId : 'NTUxNTY4YWE1NWUxMzI4',
username : 'john@globallcoach.com',
password : 'password',
init: function() {
var provision = OAuth.provision();
if(provision.hasOwnProperty('success')) {
var authenticate = OAuth.authenticate();
if(authenticate.hasOwnProperty('access_token')) {
api.AccessToken = authenticate['access_token'];
}
}
},
provision: function() {
var response;
$.ajax({
async: false,
url : 'http://customer-server-2.dev/oauth/provision/.json',
type : 'get',
data : {
title : OAuth.title,
client_id : OAuth.clientId
},
success:function(data) {
response = jQuery.parseJSON(data);
},
});
return response;
},
authenticate: function() {
var response;
$.ajax({
async: false,
url : 'http://customer-server-2.dev/oauth/token.json',
type : 'get',
data : {
'grant_type' : 'password',
'username' : OAuth.username,
'password' : OAuth.password,
'client_id' : OAuth.clientId,
},
success:function(data) {
response = data;
}
});
return response;
},
}
/**
* Exercute & return
*/
api.router = new Router();
OAuth.init();
return api;
})();
已解决!我需要确保在Rest AppController上,我需要在api范围内的操作上定义$this->Auth->allow()
。
相关文章:
- jQuery使用api获取typeform结果
- 使用API在我的网站Google-Map上显示搜索地址的纬度和经度
- 使用api在facebook页面上发布图片或状态
- 无法使用API-node.js从Google Drive下载文件
- 如何使用API从多个IMDB ID获取电影信息
- PHP中的Curl请求-使用API
- 使用 API 注册创建节点.js API 框架
- 使用 API javascript 设置 AngularJS 模板
- 计算使用API观看youtube视频的百分比
- 如何使用API URL的JSON.parse将JSON字符串更改为Javascript对象
- 使用API的Chargify问题:;不允许访问”;
- 如何使用API从SMTP服务器访问和提取电子邮件
- 使用API平台打开Chrome扩展
- 如何使用Api v3在谷歌地图上放置图像
- 使用数组对象从javascript使用api到javascript使用sightly
- 在javascript中使用API v3检索Google日历事件
- 使用 API 下载文件不起作用
- 使用 API v3.0 为特定视频创建 Youtube“喜欢”按钮
- 是否可以确定用户是否完成了使用 API 观看嵌入的 youtube 视频
- JWPlayer在使用API加载方法时无法在iPad / iPhone上播放