5

我一直在尝试将 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();
    }

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

4

1 回答 1

1

我有一个类似的问题,所以修改了样本以适应。包含的顺序似乎也影响了它。这对我有用:

  1. 在 google API 控制台中 - https://code.google.com/apis/console -
  2. 设置新项目,保存客户端密钥和 API 密钥。
  3. 将回调 URI 设置为调用文件。
  4. 在“服务”下打开分析 API、谷歌云存储和预测 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);
    }

希望它可以帮助您解决您的问题。-乔尔

于 2012-06-22T05:13:15.027 回答