LoginSignup
8
10

More than 5 years have passed since last update.

Google Drive にファイルを保存する

Posted at

以下の手順を踏む

デベロッパーコンソールでアプリを登録する

Web でポチポチする。

  1. https://console.developers.google.com/project に行く
  2. 新規プロジェクトを作成する
  3. 暫し待つ
  4. できたら、APIs & auth を選択する
  5. Credentials から新しい Client ID を作成する
  6. Installed Application を選択
  7. 必須項目を入れる
  8. APPLICATION TYPE を Installed Application にする
  9. INSTALLED APPLICATION TYPE を Other にする
  10. 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 が取れる。

8
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
10