Unauthorized API calls on Google Plus platform

In the process of creating a prototype with two basic pages and Google Plus integration, I encounter an issue. The first page includes a "login" button, while the second has a link. Upon clicking the login button, the following code is executed:

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

The signinCallback function is structured 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']);
  }
}

Upon successful sign-in and permission granting by the user, the token is stored for future calls to obtain a people list, which functions properly.

However, upon transitioning to the second page and attempting to make the same API call as 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);
  });
});

An error occurs with the message:

Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.

I assumed that using "setToken" after initial authentication should bypass the need for authentication for subsequent calls. What could be possibly going wrong in my implementation?

Answer №1

If there are indeed two separate pages rather than one page making server calls for additional data, then each page operates within its own JavaScript environment. This means that the gapi object exists as a distinct copy on each page, and any authentication set on the first page does not carry over to the gapi object on the second page. The token is associated with a specific JavaScript object, not a session.

In the case of using something like Google+ Sign In, placing the button on each page would result in each page obtaining its own token upon user visitation. However, this method can be inefficient as it requires a server round-trip each time.

An alternative approach could involve storing the authentication token in temporary or session local storage, but precautions must be taken to prevent potential security risks from token leakage.

Various solutions exist depending on how the authenticated user will be utilized within your client application.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using jQuery to store the name of a Div in a variable and subsequently invoking it in a function

Recently, I've been grappling with a task involving storing a div name in a variable for easier editing and incorporating it into standard actions like show/hide. My code functions perfectly without the variables, but introducing them causes the div ...

Jest test encounters an error due to an unexpected token, looking for a semicolon

I've been working on a Node project that utilizes Typescript and Jest. Here's the current project structure I have: https://i.stack.imgur.com/TFgdQ.png Along with this tsconfig.json file "compilerOptions": { "target": "ES2017", "modu ...

Troubleshooting a setTimeout filter problem in Vue

Implementing a notification system in Vue has been my latest project. I've created a Notifications component to store error messages that I want to display. data(){ return { alerts: { error: [] } ...

Is it possible to utilize a map in Python or incorporate other advanced functions to efficiently manage indices?

When working with higher order functions in JavaScript, we have the ability to access the element, index, and iterable that the function is being performed on. An example of this would be: [10,20,30].map(function(element, index, array) { return elem + 2 ...

Burst or displace meshes outward from the center

I am attempting to separate all the meshes that make up the loaded obj file from each other in order for the user to easily view each mesh individually. I am striving to replicate a similar functionality as shown in this example: Demo To achieve this ef ...

How to italicize a portion of output using JavaScript

There are numerous inquiries on the internet and on this site regarding how to make italic fonts, however, none specifically address how to italicize only a section of a string. In my scenario, I have a form where I input the book title, author's nam ...

What is the best way to retrieve a specific key from a JavaScript Map containing an array?

I am currently iterating through a two-dimensional array to populate a map. The map is using the [i,j] indices as keys and the corresponding arr[i][j] values as values: const arrMap = new Map() for(let i = 0; i < arr.length; i++){ for(let j = 0 ...

Applying Tailwind styles to dynamically inserted child nodes within a parent div

Currently, I am in the process of transitioning my website stacktips.com from using Bootstrap to Tailwind CSS. While initially, it seemed like a simple task and I was able to replace all the static HTML with tailwind classes successfully. However, now I ha ...

Dealing with special characters in a json XMLHttpRequest: A guide

I'm currently grappling with the best approach for handling special or foreign characters within an AJAX request. My current test code is as follows: var xmlhttp = new XMLHttpRequest(); xmlhttp.open("POST","test.json",true); xmlhttp.setRequestHeader ...

What is the best way to manage a promise's response?

I'm currently facing a situation where I need to create a petition to an endpoint and retrieve the URL of the backend that I intend to use. My setup involves a file with the API configuration where I instantiate axios. Previously, my approach was sim ...

Troubles with IE7 regarding jQuery [clicking and changing events]

My code snippets are designed to loop through cached JSON data on the 1st click function and display any values that exist for a specific id. The 2nd change function captures when elements' values change (e.g., from yes to no). Since these elements a ...

Showing chosen checkbox information using jQuery ajax

When a user enters more than 3 characters into a text box for live search, results are displayed from the database via an AJAX response as checkboxes with names. I want selected checkboxes to display in a separate div below so users can search multiple dat ...

retrieve data from an external HTML file using JavaScript

Can someone assist me? I am working on a webpage that generates a table displaying high tide values in Venice from this link: http://93.62.201.235/maree/ESPORTAZIONI/MESE/Stazione_PuntaSalute_CanalGrande.html. I am trying to extract a specific value (the t ...

Steps for ensuring a div component appears on top of any other component (like h1 or p)

The outcome of the code below is displayed in this image: https://i.stack.imgur.com/CPDqC.png Is it possible to make the dropdown overlap on top of the 'HELLO's text, hiding the h1 and p tags? I have tried using z-index without success. Making ...

Displaying subtotal in a list using Vue.js and conditional rendering with v-if statement

Seeking guidance on calculating a total for a vue.js list that contains invoice items. To illustrate, let's consider a scenario where a table of invoice items is being rendered. Here is the code snippet: <table> <template v-for="(invoice_ite ...

Validation method in jQuery for a set of checkboxes with distinct identifiers

I am faced with a situation where I have a set of checkboxes that, due to the integration with another platform, must have individual names even though they are all interconnected. <div class="form-group col-xs-6 checkbox-group"> <label cla ...

Bootstrap 4 Card Body Spinner Overlay with Flex Alignment

Seeking to center a spinner both vertically and horizontally within a bootstrap 4 card body. Despite trying my-auto, justify-content-center & align-items-center, it seems like I'm missing something. I've double-checked the display types and ...

Show text using AJAX when the form is submitted

I'm in the process of creating a basic form with a submit button (see below). My objective is to allow users to enter text into the input box, click submit, and have the page refresh while displaying the entered text in a div element. The user's ...

Having trouble dynamically adding elements to the Semantic-UI Accordion component

I have a challenge involving inserting dynamic content into a Semantic-UI Accordion. My goal is to display JSON data as an Accordion in the HTML. Below is the script and HTML I am using for this purpose: <script language='javascript'> ...

Increase the size of images on the same screen with a click using WP Metas

Currently, I am utilizing WP Metaslider for a slideshow/gallery presentation. While it is functioning well, I would like to enhance the user experience by allowing the images in the slider to enlarge within the slide when clicked on, rather than opening in ...