8
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

GoogleColaboratoryからスプレッドシートを操作するための認証が通らなくなった話

Posted at

はじめに

GoogleColaboratoryからスプレッドシートを操作する際、以下のような認証を実装していました。

from google.colab import auth
from oauth2client.client import GoogleCredentials
import gspread

auth.authenticate_user()
gc = gspread.authorize(GoogleCredentials.get_application_default())

2022年の2月までは上記実装で動いていたのですが、2022/4/28に久しぶりに実行したところ、
下記のエラーが発生し、認証処理が通らなくなりました。

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-67cdf2a8c5fe> in <module>()
      4 
      5 auth.authenticate_user()
----> 6 gc = gspread.authorize(GoogleCredentials.get_application_default())

2 frames
/usr/local/lib/python3.7/dist-packages/gspread/utils.py in convert_credentials(credentials)
     59 
     60     raise TypeError(
---> 61         'Credentials need to be from either oauth2client or from google-auth.'
     62     )
     63 

TypeError: Credentials need to be from either oauth2client or from google-auth.

解決策

以下のような実装にすることで認証が通るようになりました。

from google.colab import auth
from google.auth import default
import gspread

auth.authenticate_user()
creds, _ = default()
gc = gspread.authorize(creds)

原因

調べてみると、gspreadでは以前からoauth2clientが非推奨となっていたようでした。

8
5
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
8
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?