3
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

posted at

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

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

発生したバージョン

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
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
3
Help us understand the problem. What are the problem?