TL;DR
- google-api-ruby-client のサンプルコードを動かす
- cli/webのsampleコードがあるが、今回はcli版
- README
コードを取得してGemのinstall
git clone git@github.com:google/google-api-ruby-client.git
cd google-api-ruby-client/samples/cli
bundle install --path=vendor/bundle
認証keyなどを取得
- https://console.developers.google.com
- サンプルを動かしたいAPIの権限を付与してcredentialを発行する
.envに発行したkeyなどを設定
GOOGLE_API_KEY=xxxxxx
GOOGLE_CLIENT_ID=xxxxxxx.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET=XXXXXXXXX
GOOGLE_APPLICATION_CREDENTIALS=~/Downloads/ruby-samples-cred.json
サンプルコードを動かす
-> % bundle exec google-api-samples
Commands:
google-api-samples analytics # Analytics samples
google-api-samples androidpublisher # Androidpublisher samples
google-api-samples bigquery # Bigquery samples
google-api-samples calendar # Calendar samples
google-api-samples drive # Drive samples
google-api-samples gmail # Gmail samples
google-api-samples help [COMMAND] # Describe available commands or one specific command
google-api-samples pubsub # PubSub samples
google-api-samples sheets # Sheets samples
google-api-samples translate # Translate samples
google-api-samples youtube # YouTube samples
OAuth認証
- OAuth_tokenがない場合は、tokenを取得するためのurlが表示されるので、ブラウザでそのurlを開き、認証するアカウントで承認するとauthorization condeが表示されるので、それを貼り付ける
-> % bundle exec ./google-api-samples sheets get_values --spreadsheet_key='1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms' --range='Class Data!A1:F'
Open the following URL in your browser and authorize the application.
https://accounts.google.com/o/oauth2/auth?access_type=offline&approval_prompt=force&client_id=xxxxxxx.apps.googleusercontent.com&include_granted_scopes=true&redirect_uri=urn:ietf:wg:oauth:2.0:oob&response_type=code&scope=https://www.googleapis.com/auth/spreadsheets.readonly
Enter the authorization code: PASTE_AUTHORIZATION_CODE
Translate
-> % bundle exec ./google-api-samples translate --target=ja Hello
こんにちは
SpreadSheet
-> % bundle exec ./google-api-samples sheets get_values --spreadsheet_key='1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms' --range='Class Data!A1:F'
Student Name Gender Class Level Home State Major Extracurricular Activity
Alexandra Female 4. Senior CA English Drama Club
Andrew Male 1. Freshman SD Math Lacrosse
Anna Female 1. Freshman NC English Basketball
Becky Female 2. Sophomore SD Art Baseball
Benjamin Male 4. Senior WI English Basketball
Carl Male 3. Junior MD Art Debate
Carrie Female 3. Junior NE English Track & Field
Dorothy Female 4. Senior MD Math Lacrosse
Dylan Male 1. Freshman MA Math Baseball
Edward Male 3. Junior FL English Drama Club
Ellen Female 1. Freshman WI Physics Drama Club
Fiona Female 1. Freshman MA Art Debate
John Male 3. Junior CA Physics Basketball
Jonathan Male 2. Sophomore SC Math Debate
Joseph Male 1. Freshman AK English Drama Club
Josephine Female 1. Freshman NY Math Debate
Karen Female 2. Sophomore NH English Basketball
Kevin Male 2. Sophomore NE Physics Drama Club
Lisa Female 3. Junior SC Art Lacrosse
Mary Female 2. Sophomore AK Physics Track & Field
Maureen Female 1. Freshman CA Physics Basketball
Nick Male 4. Senior NY Art Baseball
Olivia Female 4. Senior NC Physics Track & Field
Pamela Female 3. Junior RI Math Baseball
Patrick Male 1. Freshman NY Art Lacrosse
Robert Male 1. Freshman CA English Track & Field
Sean Male 1. Freshman NH Physics Track & Field
Stacy Female 1. Freshman NY Math Baseball
Thomas Male 2. Sophomore RI Art Lacrosse
Will Male 4. Senior FL Math Debate
BigQuery
-> % bundle exec ./google-api-samples bigquery list_datasets
id kind
publicdata:samples bigquery#dataset
-> % bundle exec ./google-api-samples bigquery list_tables
id kind
publicdata:samples.github_nested bigquery#table
publicdata:samples.github_timeline bigquery#table
publicdata:samples.gsod bigquery#table
publicdata:samples.natality bigquery#table
publicdata:samples.shakespeare bigquery#table
publicdata:samples.trigrams bigquery#table
publicdata:samples.wikipedia bigquery#table
-> % bundle exec ./google-api-samples bigquery list_table_data
Especially for You 2219863 0 241314803 88.156.226.45 1222519667 [[]] 5211
AVUS 176958 0 1425182 80.131.193.37 1050002575 Berlin's former superspeedway racetrack 2270
Paul McCartney 13736167 0 6333039 65.73.182.74 1096832410 /* Achievements & World Records */ 22459
Taking Chances Tour 11374422 0 209696574 85.107.38.14 1209737703 /* Record sellouts */ 35746
Niagara IceDogs 10875143 0 264688296 216.59.231.229 1232210110 /* Current roster */ 19259
Martin Gerber 3230540 0 101271106 71.7.210.116 1169012210 /* International play */ 7260
Swat, Pakistan 1236283 0 289918255 115.186.108.237 1242322364 /* April-May 2009 Pakistani offensive */ 32901
Dorian 13959190 0 298095668 83.55.97.26 1245749953 /* Rock bands */ 1733
Difference 229878 0 115281045 195.41.76.66 1173951541 /* See also */ 1157
Le Bonheur Children's Medical Center 6207483 0 232751932 198.254.16.200 1219089190 /* Pedi-Flight Team */ 4695
サンプルコードの実態
samples/cli/lib/samples/xxx.rb
- うまく動かなかったり、引数などを確認する場合は、コードを見たほうが早い
- すべてのAPIに対応するサンプルコードはないので作成して、pull requestをなげると取り込んでもらえる