简体   繁体   English

对于 javascript 调用 api “gapi.auth.authorize” 的 google 分析没有得到执行。

[英]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.我一直在尝试将 Google Analytics Core Reporting API 与 JavaScript 结合使用。 I am new to it and I was trying to use the sample code provided by google for core_reporting_api_v3 .我是新手,我试图使用谷歌为core_reporting_api_v3提供的示例代码。 But after the core_reporting_api_v3.html file is ran it calls auth_util.js .但在core_reporting_api_v3.html文件运行后,它调用auth_util.js

Code from auth_utils.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();
    }

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.checkAuth()函数中,调用了 google api gapi.auth.authorize其中包含客户端 ID、范围、立即数(都尝试了 :true/false)和回调函数。 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.有人可能认为问题出在凭据上,但我使用 python 使用了相同的凭据并成功获得了结果。 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?是否有任何教程可以在 javascript 中以原始形式编写这个gapi.auth.authorize调用作为 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 -在谷歌 API 控制台 - https://code.google.com/apis/console -
  2. setup new project, saved client Key and API key.设置新项目,保存客户端密钥和 API 密钥。
  3. Set the callback URI to the calling file.将回调 URI 设置为调用文件。
  4. Under "Services" Turned on Analytics API, Google Cloud Storage and Prediction API.在“服务”下打开分析 API、谷歌云存储和预测 API。 After this it started Authing OK.在此之后它开始Authing OK。

Here is the code i used that worked for me.这是我使用的对我有用的代码。

Within calling page- as saved as RedirectURI in the console:在调用页面内 - 在控制台中保存为 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);
    }

Hope it helps you with your issue.希望它可以帮助您解决您的问题。 -Joel -乔尔

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 gapi.auth.authorize:TypeError:cv(...)为null - gapi.auth.authorize: TypeError: cv(…) is null 带立即数的gapi.auth.authorize:true不起作用 - gapi.auth.authorize with immediate: true is not working gapi.auth.authorize:TypeError:_.Uu不是函数 - gapi.auth.authorize: TypeError: _.Uu is not a function Google OAuth gapi.auth.authorize X-Frame-Options:SAMEORIGIN - Google OAuth gapi.auth.authorize X-Frame-Options: SAMEORIGIN 使用gapi.auth.authorize时访问被拒绝错误 - Access Denied Error when using gapi.auth.authorize 检测用户在使用gapi.auth.authorize时是否关闭弹出窗口 - Detect if user closes popup when using gapi.auth.authorize 在node-webkit应用程序上通过Google登录时,不会调用gapi.auth.authorize回调 - gapi.auth.authorize callback doesn't get called when doing sign-in via Google on node-webkit app 如何将login_hint传递给gapi.auth.authorize? - How do I pass login_hint to gapi.auth.authorize? 为什么带有“ immediate:false”的gapi.auth.authorize不关闭弹出窗口并触发回调? - Why gapi.auth.authorize with “immediate: false” doesn't close popup and fire callback? Google Analytics(分析)嵌入式API gapi.analytics.auth.isAuthorized()始终返回false - Google Analytics Embed API gapi.analytics.auth.isAuthorized() always returns false
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM