对于javascript,对api“gapi.auth.authorize”的调用不会执行Google Analytic
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()
函数中,有一个对谷歌 api gapi.auth.authorize
的调用,其中包含客户端 ID、范围、即时(尝试过 :true/false)和回调函数。它应该弹出一个授权窗口进行用户授权。之后调用回调函数。但是这个弹出窗口永远不会出现。请帮我解决这个问题,我不明白问题是什么。有人可能会认为问题出在凭据上,但我使用 python 使用相同的凭据并成功获得结果。有什么方法可以在浏览器中跟踪后面的进程,例如:正在进行什么调用以及进程卡在哪里?是否有任何教程可以在javascript中以原始形式编写此gapi.auth.authorize
调用作为REST API?
我遇到了类似的问题,因此修改了示例以适应。此外,包含的顺序似乎也影响了它。以下是对我有用的方法:
- 在谷歌API控制台 https://code.google.com/apis/console 中 -
- 设置新项目,保存客户端密钥和 API 密钥。
- 将回调 URI 设置为调用文件。
- 在"服务"下,打开了分析 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);
}
希望它对您的问题有所帮助。-乔尔
相关文章:
- 通过ajax从客户端调用C#方法来执行C#方法
- ajax调用获胜't同步执行
- 使用Node中的Q promise库对值数组顺序调用/执行相同的函数;并返回带有结果的新数组/集合
- 如何在异步调用执行之前阻止执行
- 表单提交成功后调用/执行JQuery函数
- ng 类指令调用执行两次
- 在猫鼬中创建承诺时,不会调用执行器
- 资源查询调用执行计时
- 是使用每个方法调用执行的对象函数
- 是否可以在ioredis中使用单个.sadd/.zadd调用执行多个SADD或ZADD分配
- 对函数调用执行双击功能
- nodejs中的函数调用执行
- 使用AJAX调用执行Javascript
- 使用javascript ajax调用执行PHP脚本
- 通过ajax调用执行动态javascript
- 是否可以通过AJAX调用执行后台PHP服务器任务,该任务不会锁定您的网站
- 停止从外部文件调用执行代码
- JavaScript匿名函数立即调用/执行(表达式与声明)
- JQuery.Load()是否将标记呈现和Java脚本作为单个阻塞调用执行
- 对每个调用执行JavaScript属性计算