以下の手順を踏む
デベロッパーコンソールでアプリを登録する
Web でポチポチする。
- https://console.developers.google.com/project に行く
- 新規プロジェクトを作成する
- 暫し待つ
- できたら、APIs & auth を選択する
- Credentials から新しい Client ID を作成する
- Installed Application を選択
- 必須項目を入れる
- APPLICATION TYPE を Installed Application にする
- INSTALLED APPLICATION TYPE を Other にする
- APIs & auth の APIs から、Drive API を ON にする
"google-api-client" を入れる
Ruby から Google API を使うには、"google-api-client"を使う。
$ gem install google-api-client
コードを書く
require 'google/api_client'
puts "Uploading to Google Drive..."
drive_client = Google::APIClient.new(
:application_name => "Application Name",
:application_version => "1.0.0"
)
drive = drive_client.discovered_api('drive', 'v2')
auth = drive_client.authorization
# client_id と client_secret はコンソールから持ってくる
auth.client_id = "your client id"
auth.client_secret = "your client secret"
auth.scope = "https://www.googleapis.com/auth/drive.file"
auth.redirect_uri = "your_redirect_uri"
uri = auth.authorization_uri
# ブラウザを開いて URL をコピペし、認可してもらう
puts " Open browser with this uri: #{uri}"
# authorization code を入れてもらう
$stdout.write "Enter authorization code: "
auth.code = gets.chomp
auth.fetch_access_token!
# CSV を上げてみる
file = drive.files.insert.request_schema.new({
'title' => filename,
'description' => 'Mixpanel export CSV',
'mimeType' => 'text/csv'
})
puts " Uploading CSV..."
filename = "hoge.csv"
media = Google::APIClient::UploadIO.new(filename, 'text/csv')
result = drive_client.execute(
:api_method => drive.files.insert,
:body_object => file,
:media => media,
:parameters => {
'uploadType' => 'multipart',
'alt' => 'json'})
# alternateLink を出力すると、ブラウザでファイルを開くことが出来る
puts " URL: #{result.data.alternateLink}"
puts " Done!"
リフレッシュトークンが得られれば、アクセストークンの期限が切れても自動で新しいアクセストークンをもらってアップロードを継続できる。だいぶ邪悪だ。
require 'google/api_client'
puts "Uploading to Google Drive..."
drive_client = Google::APIClient.new(
:application_name => "Application Name",
:application_version => "1.0.0"
)
drive = drive_client.discovered_api('drive', 'v2')
auth = drive_client.authorization
# client_id と client_secret はコンソールから持ってくる
auth.client_id = "your client id"
auth.client_secret = "your client secret"
auth.scope = "https://www.googleapis.com/auth/drive.file"
auth.refresh_token = "your refresh token"
auth.fetch_access_token!
# CSV を上げてみる
file = drive.files.insert.request_schema.new({
'title' => filename,
'description' => 'Mixpanel export CSV',
'mimeType' => 'text/csv'
})
puts " Uploading CSV..."
filename = "hoge.csv"
media = Google::APIClient::UploadIO.new(filename, 'text/csv')
result = drive_client.execute(
:api_method => drive.files.insert,
:body_object => file,
:media => media,
:parameters => {
'uploadType' => 'multipart',
'alt' => 'json'})
# alternateLink を出力すると、ブラウザでファイルを開くことが出来る
puts " URL: #{result.data.alternateLink}"
puts " Done!"
このままだと、認可したユーザのroot
に保存されてしまうので、別のフォルダにしたければ以下のようにする。
# CSV を上げてみる
file = drive.files.insert.request_schema.new({
'title' => filename,
'description' => 'Mixpanel export CSV',
'mimeType' => 'text/csv',
'parents' => [{
'id' => 'parent folder id'
}]
})
parent folder id
は、Google Drive をブラウザで開いた時の URL の末尾の長い文字列。スラッシュで区切ってどんどん階層を掘っていけるようになっているので、入れたいフォルダを開いて最後のスラッシュから末尾までをコピペするとフォルダの ID が取れる。