[英]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:
这是对我有用的:
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.