5

JavaScript で Google Analytics Core Reporting API を使用しようとしています。私はそれが初めてで、Google が提供する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. [Services] の下で、Analytics API、Google Cloud Storage、および Prediction 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 に答える