Last active
August 29, 2015 14:09
-
-
Save nikushi/734fc1d86e5db7403e69 to your computer and use it in GitHub Desktop.
Google Spreadsheet を Ruby で OAuth を使って操作するサンプル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'http://rubygems.org' | |
gem 'google-api-client', '>= 0.6' | |
gem 'google_drive' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# | |
# 1. Developers Console にログインして、プロジェクトを作成する | |
# 2. API -> Drive API を有効にする | |
# 3. クライアントIDを作成する | |
# 3-1. アプリケーションの種類は Installed application を選択する | |
# 3-2. タイプは **その他** を選択 | |
# 4. 作成したクライアントIDのページにある **JSONをダウンロード** を押して、JSONファイルをダウンロードする | |
# アプリのディレクトリに client_secrets.json におく | |
# 5. bundle install | |
# 6. bundle exec ruby spreadsheet-sample.rb | |
# | |
# Copyright (C) 2012 Google Inc. | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
require 'rubygems' | |
require 'google/api_client' | |
require 'google/api_client/client_secrets' | |
require 'google/api_client/auth/file_storage' | |
require 'google/api_client/auth/installed_app' | |
require 'google_drive' | |
CREDENTIAL_STORE_FILE = "#{$0}-oauth2.json" | |
# Handles authentication and loading of the API. | |
def setup() | |
client = Google::APIClient.new(:application_name => 'Ruby spreadsheet sample', | |
:application_version => '1.0.0') | |
# FileStorage stores auth credentials in a file, so they survive multiple runs | |
# of the application. This avoids prompting the user for authorization every | |
# time the access token expires, by remembering the refresh token. | |
# Note: FileStorage is not suitable for multi-user applications. | |
file_storage = Google::APIClient::FileStorage.new(CREDENTIAL_STORE_FILE) | |
if file_storage.authorization.nil? | |
client_secrets = Google::APIClient::ClientSecrets.load | |
# The InstalledAppFlow is a helper class to handle the OAuth 2.0 installed | |
# application flow, which ties in with FileStorage to store credentials | |
# between runs. | |
flow = Google::APIClient::InstalledAppFlow.new( | |
:client_id => client_secrets.client_id, | |
:client_secret => client_secrets.client_secret, | |
:scope => %w( | |
https://www.googleapis.com/auth/drive | |
https://docs.google.com/feeds/ | |
https://docs.googleusercontent.com/ | |
https://spreadsheets.google.com/feeds/ | |
), | |
) | |
client.authorization = flow.authorize(file_storage) | |
else | |
client.authorization = file_storage.authorization | |
end | |
return client | |
end | |
client = setup() | |
session = GoogleDrive.login_with_oauth(client.authorization.access_token) | |
ws = session.spreadsheet_by_key("YOUR_SPREADSHEET_ID_HERE").worksheets[0] | |
# dump a cell | |
p ws[1,1] | |
# update a cell | |
ws[5,5] = "new" | |
ws.save | |
# dump all cells | |
pp ws.rows |
CREDENTIAL_STORE_FILE
で指示されたファイルが存在しない場合、新しいaccess tokenを取得するためにブラウザを開いて許可された後、CREDENTIAL_STORE_FILE
に access tokenをキャッシュする。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
client_secrets.json
はGoogle::APIClient::ClientSecrets.load
時に読まれる。.load
の引数で指定して任意のパスにも置ける。Google::APIClient::ClientSecrets.load('config/client_secrets.json')