对于javascript,对api“gapi.auth.authorize”的调用不会执行Google Analytic

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

本文关键字:调用 执行 Analytic Google authorize javascript api auth gapi 对于      更新时间:2023-09-26

我一直在尝试将Google Analytics Core Reporting API与JavaScript一起使用。我是新手,我正在尝试使用谷歌提供的示例代码进行core_reporting_api_v3。但是在运行core_reporting_api_v3.html文件后,它会调用auth_util.js

来自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();
    }

checkAuth()函数中,有一个对谷歌 api gapi.auth.authorize的调用,其中包含客户端 ID、范围、即时(尝试过 :true/false)和回调函数。它应该弹出一个授权窗口进行用户授权。之后调用回调函数。但是这个弹出窗口永远不会出现。请帮我解决这个问题,我不明白问题是什么。有人可能会认为问题出在凭据上,但我使用 python 使用相同的凭据并成功获得结果。有什么方法可以在浏览器中跟踪后面的进程,例如:正在进行什么调用以及进程卡在哪里?是否有任何教程可以在javascript中以原始形式编写此gapi.auth.authorize调用作为REST API?

我遇到了类似的问题,因此修改了示例以适应。此外,包含的顺序似乎也影响了它。以下是对我有用的方法:

  1. 在谷歌API控制台 https://code.google.com/apis/console 中 -
  2. 设置新项目,保存客户端密钥和 API 密钥。
  3. 将回调 URI 设置为调用文件。
  4. 在"服务"下,打开了分析 API、Google Cloud Storage 和预测 API。在此之后,它开始了 Authing OK。

这是我使用的代码,对我有用。

在调用页面中 - 在控制台中保存为 RedirectURI

<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);
    }

希望它对您的问题有所帮助。-乔尔