LoginSignup
3
3

More than 5 years have passed since last update.

Google::APIClient::FileStorageのwrite_credentialsでこける

Posted at

を参考にやってみたけどうまくいかなかったのでメモ。

発生したバージョン

google-api-clientの0.8.1.1で発生。

エラー内容

`write_credentials': undefined method `authorization=' for #<Google::APIClient::FileStorage:0x007fbecca58b78> (NoMethodError)

みたいなエラーが出て認証情報を保存できなかった。

google-api-client-0.8.1.1/lib/google/api_client/auth/file_storage.rb

を見てみるとdeprecatedGoogle::APIClient::StorageGoogle::APIClient::FileStoreを使えと書いてあった

というわけで

のsetupメソッドを以下のようにしたら動いた

def setup()
  client = Google::APIClient.new(application_name: 'Sample', application_version: '1.0.0')
  store = Google::APIClient::FileStore.new(CREDENTIAL_STORE_FILE)
  storage = Google::APIClient::Storage.new(store)
  storage.authorize

  if storage.authorization.nil?
    client_secrets = Google::APIClient::ClientSecrets.load

    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
    storage.write_credentials(client.authorization)
  else
    client.authorization = storage.authorization
  end

  return client
end
3
3
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
3
3