簡體   English   中英

對於 javascript 調用 api “gapi.auth.authorize” 的 google 分析沒有得到執行。

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

我一直在嘗試將 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()函數中,調用了 google 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、谷歌雲存儲和預測 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);
    }

希望它可以幫助您解決您的問題。 -喬爾

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM