使用Oauth使用Javascript访问Api

Accessing Api with Javascript using Oauth

本文关键字:使用 Api 访问 Javascript Oauth      更新时间:2023-09-26

使用简单的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()