简体   繁体   中英

For javascript the call to api “gapi.auth.authorize” for google analytics not getting executed.

I have been trying to use Google Analytics Core Reporting API with JavaScript. I am new to it and I was trying to use the sample code provided by google for core_reporting_api_v3 . But after the core_reporting_api_v3.html file is ran it calls auth_util.js .

Code from auth_utils.js:

function checkAuth() 
{
    gapi.auth.authorize({client_id: clientId, scope: scopes}, handleAuthResult);
}

function handleAuthResult(authResult) 
{
    alert("made it");
    if (authResult) 
    {
        gapi.client.load('analytics', 'v3', handleAuthorized);
    } 
    else 
    {
        handleUnAuthorized();
    }

Here in checkAuth() function there is a call made to google api gapi.auth.authorize with the client Id, scopes, immediate(tried for both :true/false) and callback function. And it should pop-up an authorization window for user authorization. After that the callback function is called. But this pop-up window never appears. Please help me with this, I am not getting what the problem is. One might think the problem is with credentials but I am using the same credentials using python and getting the results successfully. Is there any way I can track the process going behind in the browser like: what call is being made and where is the process getting stuck? Is there any tutorial for writing this gapi.auth.authorize call in raw form in javascript as a REST API?

i had a similar issue, so modified the samples to suit. Also the order the includes seemed to effect it. Here is what worked for me:

  1. In the google API Console-https://code.google.com/apis/console -
  2. setup new project, saved client Key and API key.
  3. Set the callback URI to the calling file.
  4. Under "Services" Turned on Analytics API, Google Cloud Storage and Prediction API. After this it started Authing OK.

Here is the code i used that worked for me.

Within calling page- as saved as RedirectURI in the console:

<script language="javascript">
    //  PURPOSE:    Load in ClientID and API key dynamically
    var cMsg;
    var cStatus     =   '';
    var clientId    =   '<?php echo $authClientID; ?>';
    var apiKey      =   '<?php echo $authDevKey; ?>';
    var scopes      =   'https://www.googleapis.com/auth/analytics.readonly';
    var scope       =   'https://www.google.com/analytics/feeds';
    var profileId   =   '';
</script> 
<!-- CORE API JS --> 
<script src="js/hello_analytics_api_v3_auth.js"></script> 
<script src="js/hello_analytics_api_v3.js"></script> 
<!-- Load the Client Library. Use the onload parameter to specify a callback function --> 
<script src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script> 


Here is the code i've used to get around it Saved in hello_analytics_api_v3_auth.js:

    function handleClientLoad() {
        gapi.client.setApiKey(apiKey);
        window.setTimeout(checkAuth,1);
    }

    //  3. Check if the user has Authenticated and Authorized
    function checkAuth() {
        // Call the Google Accounts Service to determine the current user's auth status.
        // Pass the response to the handleAuthResult callback function
        gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: true}, handleAuthResult);

    }
    //  4. Update the UI based on the user's authorization status
    function handleAuthResult(authResult) {
      if (authResult) {
        // The user has authorized access
        // Load the Analytics Client. This function is defined in the next section.
        loadAnalyticsClient();
      } else {
        // User has not Authenticated and Authorized
        handleUnAuthorized();
      }
    }
    //  3. Create An Analytics Service Object
    function loadAnalyticsClient() {
      // Load the Analytics client and set handleAuthorized as the callback function
      gapi.client.load('analytics', 'v3', handleAuthorized);
    }

Hope it helps you with your issue. -Joel

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM