code Google OAuth2 est basé sur https://developers.google.com/api-client-library/javascript/features/authenticationGoogle OAuth2 + Backbone + Require.js = Problèmes avec "cette" fixation
LoginView
a quelques fonctions, chaque fonction appelle une autre fonction.
Au moment où il obtient la fonction checkAuth
, les rendements this.handleAuthResult
non défini parce checkAuth
a été appelé à l'intérieur du setTimeout par handleClientLoad
.
Comment traiter avec this
problème de contexte? Et puis-je faire la même chose pour mes variables - scope
, clientId
, apiKey
au lieu de les faire global?
define(['underscore','jquery','backbone','text!templates/login.html','async!https://apis.google.com/js/client.js!onload'], function(_, $, Backbone, loginTpl) {
var LoginView = Backbone.View.extend({
template: _.template(loginTpl),
initialize: function() {
clientId = '';
apiKey = '';
scopes = 'https://www.googleapis.com/auth/plus.me';
this.handleClientLoad();
},
render: function() {
this.$el.html(this.template());
return this;
},
handleClientLoad: function() {
gapi.client.setApiKey(apiKey);
window.setTimeout(this.checkAuth, 1);
},
checkAuth: function() {
gapi.auth.authorize({ client_id: clientId, scope: scopes, immediate: true }, this.handleAuthResult);
},
handleAuthResult: function(authResult) {
var authorizeButton = this.$el.find('#authorize-button');
if (authResult && !authResult.error) {
console.log('Authorized!');
this.makeApiCall();
} else {
authorizeButton.onclick = this.handleAuthClick;
}
},
handleAuthClick: function(event) {
gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, this.handleAuthResult);
return false;
},
makeApiCall: function() {
gapi.client.load('plus', 'v1', function() {
var request = gapi.client.plus.people.get({
'userId': 'me'
});
request.execute(function(resp) {
var authorizeButton = this.$el.find('#authorize-button');
localStorage.isAuthenticated = true;
Backbone.history.navigate('', true);
});
});
}
});
return LoginView;
});
Cette question peut être en dehors de votre question. Comment avez-vous résolu l'authentification et la sécurité avec cette bibliothèque? Est-ce qu'il renvoie un jeton? Merci –