citizen conn
citizen conn

Reputation: 15390

Google Plus api calls not authenticated

I'm developing a prototype with two simple pages and google plus integration. I have two pages, first one with a "login" button, the second one with a link. When the user clicks on the login button, I am calling:

var params = {"client_id":"<client_id>", "scope":"https://www.googleapis.com/auth/plus.login"}; 
gapi.auth.authorize(params, signinCallback);

The signinCallback looks like this:

var signinCallback = function(authResult) {
  if (authResult['access_token']) {
    gapi.auth.setToken(authResult);

    gapi.client.load('plus','v1', function(){
      var request = gapi.client.plus.people.list({
        'userId': 'me',
        'collection': 'visible'
      });

      request.execute(function(resp) {
        console.log(resp);
      });
    });

  } else if (authResult['error']) {
      console.error('Sign-in state: ' + authResult['error']);
  }
}

So when the user clicks the button, signs in and provides permissions to the app, I'm storing the token and making a people list call. This all works perfect.

My problem is when I navigate to the second page and try to make the same call I made before:

gapi.client.load('plus','v1', function(){
  var request = gapi.client.plus.people.list({
    'userId': 'me',
    'collection': 'visible'
  });
  request.execute(function(resp) {
    console.log(resp);
  });
});

The call fails with the error: Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.

I thought when I did "setToken" after signing up originally, I wouldn't have to continue authenticating every single subsequent call, what am I doing wrong?

Upvotes: 0

Views: 624

Answers (1)

Prisoner
Prisoner

Reputation: 50701

If these are truly two different pages (as opposed to one page that has made some AJAX or other calls to your server to get additional data), then each page has a completely different JavaScript environment. This means that the gapi object is a different copy on each page, and the authentication you've set on the first page hasn't been set on the gapi object on the second page. You are not setting a token for the session - you're setting it on a specific JavaScript object.

If you are using something like the Google+ Sign In, you could put the button on each page, and each page would get its own token when the user visits it, but this is somewhat inefficient, since it also means a round-trip to the server each time.

You could probably also do something like put the authentication token into temporary/session local storage, but you should be careful in this case that the token can not leak out and cause you a security issue.

There are other potential solutions, but it really boils down to how you intend to use the authenticated user as part of your client.

Upvotes: 1

Related Questions