4
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.

posted at

updated at

Google Fusion Tablesを操作する

Google app scriptを利用するパターン

Google APIからFusion Tablesを操作するパターン

設定

  • selectは、GETするだけで簡単にとってこれる

    • https://www.googleapis.com/fusiontables/v2/query?sql=SELECT * FROM {Fusion tales id}&key={your API key}
    • 公式ドキュメント
  • insertとdeleteは認証が必要

    • Create a fusion table
    • 管理画面からOAuthクライアントを作成してJsonをダウンロード Screen Shot 2017-11-05 at 15.39.09.png
    • Fusion Tablesの共有設定から、APIのemailに対して権限を追加する必要がある。

Node

var google = require('googleapis');
var fusiontables = google.fusiontables('v2');

var key = require('./管理画面からとってきたOAuth情報.json');

fusion_table_id = "Fusion tablesのID"

// クエリはここ
insert_sql = "INSERT INTO " + fusion_table_id
insert_sql += " (column_name1, column_name2, column_name3) "
insert_sql += "VALUES (111,222,333)"

var jwtClient = new google.auth.JWT(
  key.client_email,
  null,
  key.private_key,
  ['https://www.googleapis.com/auth/fusiontables', 'https://www.googleapis.com/auth/fusiontables'],
  null
);

jwtClient.authorize(function (err, tokens) {
  if (err) {
    console.log(err);
    return;
  }

  fusiontables.query.sql({
    auth: jwtClient,
    sql:insert_sql,
    key: '<api-key>'
  }, function (err, res) {
      if (err) {
          console.log(err);
      }
      else {
          console.log(res);
      }
  });
});

Python(認証なしパターン)


import urllib.request
fusion_tables_id = "<fusion-tables-id>"
api_key = "<api-key>"
sql = "SELECT * FROM "+fusion_tables_id+"&key="+api_key
url = "https://www.googleapis.com/fusiontables/v2/query?sql="+urllib.parse.quote(sql,safe='*&=')
print(url)
webURL=urllib.request.urlopen(url).read()

data = webURL.read()
encoding = webURL.info().get_content_charset('utf-8')
JSON_object = json.loads(data.decode(encoding))
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
4
Help us understand the problem. What are the problem?