使用 JS 的服务帐户调用 Google bigquery

Posted

技术标签:

【中文标题】使用 JS 的服务帐户调用 Google bigquery【英文标题】:calling Google bigquery with service account from JS 【发布时间】:2014-04-09 21:12:03 【问题描述】:

我使用下面的代码通过使用客户帐户从 JS 调用带有图表的 Gogole bigquery。 如何为 Google 服务帐户配置相同的脚本,例如我也有 p12 密钥,但我可以在哪里传递此密钥信息。

     <html>
<head>
<script type="text/javascript" src="https://apis.google.com/js/client.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
    google.load('visualization', '1',  packages: ['geochart'] );
</script>
<script>
    // UPDATE TO USE YOUR PROJECT ID AND CLIENT ID
    var project_id = 'xxxxx';
    var client_id = 'xxx.apps.googleusercontent.com';

    var config = 
        'client_id': client_id,       
        'scope': 'https://www.googleapis.com/auth/bigquery'
    ;

    function runQuery() 
        var request = gapi.client.bigquery.jobs.query(
            'projectId': project_id,
            'timeoutMs': '30000',
            'query': 'SELECT state, AVG(mother_age) AS theav FROM [publicdata:samples.natality] WHERE year=2000 AND ever_born=1 GROUP BY state ORDER BY theav DESC;'
        );
        request.execute(function (response) 
            var stateValues = [["State", "Age"]];
            $.each(response.result.rows, function (i, item) 
                var state = item.f[0].v;
                var age = parseFloat(item.f[1].v);
                var stateValue = [state, age];
                stateValues.push(stateValue);
            );
            var data = google.visualization.arrayToDataTable(stateValues);
            var geochart = new google.visualization.GeoChart(
                document.getElementById('map'));
            geochart.draw(data,  width: 556, height: 347, resolution: "provinces", region: "US" );
        );
    

    function auth() 
        gapi.auth.authorize(config, function () 
            gapi.client.load('bigquery', 'v2', runQuery);
            $('#client_initiated').html('BigQuery client initiated');
        );
        $('#auth_button').hide();
    
</script>
</head>

<body>
<h2>Average Mother Age at First Birth in 2000</h2>
<button id="auth_button" onclick="auth();">Authorize</button>
<button id="query_button" style="display:none;" onclick="runQuery();">Run Query</button>
<div id="map"></div>
</body>
</html>

【问题讨论】:

我从 google bigquery 文档中了解到 - 由于 JS 的安全原因,我们无法使用服务帐户。 【参考方案1】:

我从 google bigquery 文档中了解到 - 由于 JS 的安全原因,我们无法使用服务帐户。

【讨论】:

以上是关于使用 JS 的服务帐户调用 Google bigquery的主要内容,如果未能解决你的问题,请参考以下文章