我使用的是Google API(遵循Hello Analytics教程只是爲了讓我開始使用)並且無法使用授權。該過程正在對我進行身份驗證,但是當我嘗試從GA調用數據時,我收到一條錯誤消息,說我必須登錄才能訪問該數據。授權按鈕消失,因此我無法進行身份驗證,並出現「獲取會話」按鈕,但會導致錯誤。我的例子看起來完全像教程示例,直到結果被處理,所以我真的不知道爲什麼會出現這種情況。任何幫助,將不勝感激。這裏是我的代碼(由於顯而易見的原因,客戶端ID,API密鑰和GA屬性已被替換)。Google API授權失敗
var clientId = 'clientid';
var apiKey = 'apikey';
var scopes = 'https://www.googleapis.com/auth/analytics.readonly';
function makeApiCall() {
var apiQuery = gapi.client.analytics.data.ga.get({
'ids': 'id',
'start-date': '2014-06-01',
'end-date': '2014-07-01',
'metrics': 'ga:pageviews',
'dimensions': 'ga:pagePath',
'filters': 'ga:pagepath=~marketplace/seller/(*.?)/example'
});
console.dir(apiQuery);
apiQuery.execute(handleCoreReportingResults);
}
// This function is called after the Client Library has finished loading
function handleClientLoad() {
gapi.client.setApiKey(apiKey);
window.setTimeout(checkAuth,1);
}
function checkAuth() {
gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: true}, handleAuthResult);
}
function handleAuthResult(authResult) {
if (authResult) {
loadAnalyticsClient();
} else {
handleUnAuthorized();
}
}
// Authorized user
function handleAuthorized() {
var authorizeButton = document.getElementById('authorize-button');
var makeApiCallButton = document.getElementById('make-api-call-button');
makeApiCallButton.style.visibility = '';
authorizeButton.style.visibility = 'hidden';
makeApiCallButton.onclick = makeApiCall;
}
// Unauthorized user
function handleUnAuthorized() {
var authorizeButton = document.getElementById('authorize-button');
var makeApiCallButton = document.getElementById('make-api-call-button');
makeApiCallButton.style.visibility = 'hidden';
authorizeButton.style.visibility = '';
authorizeButton.onclick = handleAuthClick;
}
function handleAuthClick(event) {
gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthResult);
return false;
}
function loadAnalyticsClient() {
gapi.client.load('analytics', 'v3', handleAuthorized);
}
function handleCoreReportingResults(results) {
if (results.error) {
console.log('There was an error querying core reporting API: ' + results.message);
} else {
console.dir(results);
}
}
哪個教程?請張貼鏈接。 –
哎呦:https://developers.google.com/analytics/solutions/articles/hello-analytics-api – KRay