Google服务帐户已打开p12,用于访问令牌"

时间:2015-05-01 14:27:44

标签: oauth-2.0 google-analytics-api

我有这个代码(请参阅问题结尾),我所要做的就是放入我的访问令牌,事情是我已经下载了p12文件,每当我尝试打开它时,我就会得到一些Microsoft证书程序没有告诉我我需要的东西,我如何进入这个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>

1 个答案:

答案 0 :(得分:0)

请勿尝试使用嵌入API的服务帐户。使用正常的oauth 2,如文档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等脚本语言。