man*_*ins 5 javascript google-analytics-api
我一直在尝试将 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();
}
Run Code Online (Sandbox Code Playgroud)
在函数中,使用客户端 ID、范围、立即(尝试了:true/false)和回调函数checkAuth()对 google api 进行了调用。gapi.auth.authorize并且应该弹出一个授权窗口进行用户授权。之后回调函数被调用。但这个弹出窗口永远不会出现。请帮我解决这个问题,我不明白问题是什么。有人可能认为问题出在凭据上,但我使用 python 使用相同的凭据并成功获得结果。有什么方法可以跟踪浏览器中的进程,例如:正在进行什么调用以及进程卡在哪里?是否有任何教程可以gapi.auth.authorize在 javascript 中以原始形式编写此调用作为 REST API?
小智 1
我遇到了类似的问题,因此修改了示例以适应。包含的顺序似乎也对其产生影响。这对我有用:
这是我使用的对我有用的代码。
在调用页面中 - 在控制台中保存为 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);
}
Run Code Online (Sandbox Code Playgroud)
希望它能帮助您解决您的问题。-乔尔
| 归档时间: |
|
| 查看次数: |
7117 次 |
| 最近记录: |