I’m new to BigQuery (and to actually using Google API’s whatsoever). Trying to read a table from JavaScript code, I am clueless as to how this should be accomplished, given my current experience and the state of documentation and lack of sample code that I could locate. It is a table I’ve already populated through Google’s online console, and it can be seamlessly queried there in that online console.
Using Google’s beta JavaScript library for BigQuery, my code receives the error object that follows my actual code. It fails on getting the table handle, which is required per my understanding for actually issuing a call to read the table. I’m quite sure my code is incorrect in more than one way in terms of what API calls I’m using, for the sake of getting to read a table’s content.
Code
gapi.client.load('bigquery', 'v2', function() {
gapi.client.setApiKey('My Key for browser apps here...');
var request = gapi.client.bigquery.tables.get({'id': 'My-dataset-name.My-table-name'});
request.execute(function(response) {console.log(response, JSON.stringify(response, null))});
});
Result
{“code”:401,”message”:”Login Required”,”data”:[{“domain”:”global”,”reason”:”required”,”message”:”Login Required”,”locationType”:”header”,”location”:”Authorization”}]
I am short of deducing whether an API key (‘Key for browser apps’ as showing in my dashboard) is sufficient for authorizing access for reading data, as well as what is the syntax that should be used for the tables.get directive. Given the multitude of dimensions using this for the first time presents, I doubt I’d find out how to use the API without setting my hair on fire. What is a detailed explanation for those last two points, and what would working sample code be?
The following sample is HTML/JavaScript code for querying BigQuery, and populating the result in a map generated by Google Chart Tools. You need to update with your project ID and client ID– and register your app in the APIs console in order to use this.
This uses the US birth statistics (natality) sample data set documented here:
https://developers.google.com/bigquery/docs/dataset-natality
It uses the Google APIs Client Library for JavaScript to handle authorization and API requests to BigQuery.
The user will be directed through an OAuth approval flow the first time accessing this page (for permission to the OAuth scope defined in the ‘config’). They have to be an authorized user of the indicated project (so the project’s quota/billing can be implemented). This OAuth authorization will cause a popup to appear if the user hasn’t recently authorized, though they won’t be required to grant authorization again if authorization was previously granted. You can look into “immediate mode” if you need it to happen more transparently.
Note: if you’re trying to have the queries charged to your quota/bill without having the user go through OAuth authorization, you’ll need to setup a simple server-side proxy which proxies your requests to the API. Would be fairly simple to do this on App Engine with service accounts.