2015-05-01 85 views
1

我有這段代碼(查看問題的結尾),我所要做的就是放入我的訪問令牌中,事情是我已經下載了p12文件並且每當我嘗試打開它,我只是得到一些微軟證書程序,並沒有告訴我需要的東西,我該如何進入這個p12文件來獲取它所要求的訪問令牌?Google服務帳戶打開p12「訪問令牌」

謝謝。

<!doctype html> 
<html lang="en"> 

<head> 
<title>Google Charts</title> 
    <script> 
     (function(w,d,s,g,js,fs){ 
     g=w.gapi||(w.gapi={});g.analytics={q:[],ready:function(f){this.q.push(f);}}; 
     js=d.createElement(s);fs=d.getElementsByTagName(s)[0]; 
     js.src='https://apis.google.com/js/platform.js'; 
     fs.parentNode.insertBefore(js,fs);js.onload=function(){g.load('analytics');}; 
    }(window,document,'script')); 
    </script> 

    <script type="text/javascript" src="https://www.google.com/jsapi"></script> 

    <script> 

    gapi.analytics.ready(function() { 
    var ACCESS_TOKEN = '???'; // obtained from your service account 

    gapi.analytics.auth.authorize({ 
     serverAuth: { 
      access_token: ACCESS_TOKEN 
     } 
    }); 

    var data = new gapi.analytics.report.Data({ 
    query: { 
    ids: 'ga:????????', 
    metrics: 'ga:users,ga:sessions,ga:bounceRate', 
    'start-date': '30daysAgo', 
    'end-date': 'yesterday', 
    'output': 'dataTable', 
     } 
    }); 
    data.execute(); 

    data.on('success', function(response) { 
    var data = new google.visualization.DataTable(response.dataTable); 
    var formatter = new google.visualization.NumberFormat({fractionDigits: 2}); 

    formatter.format(data, 1); 

    var table = new google.visualization.Table(document.getElementById('test')); 
    table.draw(data); 
    }); 

    }); 
    google.load('visualization', '1', {'packages':['table']}); 
    google.setOnLoadCallback(table); 

    </script> 

</head> 

<body>   
       <div> 
        <div id="embed-api-auth-container"></div> 
        <div id="test"></div>  
       </div>   
</body> 
</html> 

回答

0

不要嘗試在嵌入API中使用服務帳戶。如圖所示,文檔Embeded API Getting started

gapi.analytics.ready(function() {  
    // Step 3: Authorize the user.  
    var CLIENT_ID = 'Insert your client ID here';  
    gapi.analytics.auth.authorize({ 
    container: 'auth-button', 
    clientid: CLIENT_ID, 
    });  
    // Step 4: Create the view selector.  
    var viewSelector = new gapi.analytics.ViewSelector({ 
    container: 'view-selector' 
    }); 

中如果你必須使用一個服務帳戶,我建議你把嵌入式API,並直接與reporting API使用腳本語言如PHP使用正常的OAuth 2。

+0

嘿,文檔還在這裏指定了一個服務器版本:https://developers.google.com/analytics/devguides/reporting/embed/v1/component-reference特別在「serverAuth.access_token」中,並進一步提供了代碼來做到這一點,我只需要找出如何獲得訪問令牌,我從P12文件中獲得了具有公鑰和私鑰的信息,但這些信息並沒有縫合成爲我過了很久的令牌。 謝謝。 – zoro724

+0

Embeded API是JavaScript它不會與一個服務帳戶一起工作 – DaImTo

相關問題