0
0

More than 1 year has passed since last update.

RailsからGoogleスプレッドシート連携させる際に認証でつまづいた点(2021/10時点)

Posted at

はじめに

よくあるRailsアプリケーションからGoogleスプレッドシートに連携するのを実装したかったのだが盛大につまづいたのでポイントをメモ
大まかな手順としては以下などを参考にしてください
https://japan.appeon.com/technical/techblog/technicalblog019/
https://techblog.hacomono.jp/entry/2020/10/21/095900

OAuth 同意画面のアプリケーションの種類で「その他」が選択肢にない

スクリーンショット 2021-10-28 18.08.54.png

「その他」がないなら「ウェブアプリケーション」だろうなぁと思ってたら落とし穴。
承認エラーになります
スクリーンショット 2021-10-28 18.03.42.png

自分の場合は「デスクトップアプリ」を選択したら承認されるようになりました。

初回認証時に、コンソールに表示される1. Open this page: にアクセスした後、コンソールに 2. Enter the authorization code shown in the page: が表示されない

Railsアプリケーション内で初回認証をしようとしていて発生。
こちらを参考にしました
https://japan.appeon.com/technical/techblog/technicalblog019/

別ディレクトリに以下を置いて

Gemfile

source 'https://rubygems.org'

gem 'google_drive'
main.rb
require "google_drive"

session = GoogleDrive::Session.from_config("config.json")

# 事前に書き込みたいスプレッドシートを作成しておく
sheets = session.spreadsheet_by_key("xxxxxxxx").worksheets[0]

# スプレッドシートへの書き込み
sheets[1,1] = "hello world!!"

# シートの保存
sheets.save
config.json
{
  "client_id": "xxxxxxxx.apps.googleusercontent.com",
  "client_secret": "xxxxxxxxxxxxxxxxxxxxxxxx"
}

下記コマンドを実行

$ bundle install
$ bundle exec ruby main.rb

コンソールに以下が表示され、認証コードがペーストできるように

2. Enter the authorization code shown in the page: 
0
0
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
0
0