문제

I used the below code to call the Gogole bigquery with chart from JS by using client account. How to configure the same script for the Google service account, like and I have p12 key also, but where i can pass this key information.

     <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>
도움이 되었습니까?

해결책

I got to know from google bigquery documentation that - because of security reasons form JS we can't able to use service accounts.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top