gcpug / nouhau

Google Cloud Platformのノウハウを共有するRepository
https://gcpug.jp
MIT License
653 stars 23 forks source link

bq queryでGoogle Driveのファイルを読み込む時の認証方法 #89

Open sinmetal opened 5 years ago

sinmetal commented 5 years ago

bq queryで Google Driveのファイルに対して、Queryを実行する 時に認証を少し工夫する必要がある。

単純に以下のようにコマンドを実行するとGoogle DriveのScopeが無いと言われる。 単純にgcloud auth loginした場合は、GCPのScopeをFullで取得しているだけなので、G Suite系のScopeを持っていないからだ。

$ bq query --nouse_legacy_sql 'SELECT * FROM `gcpug.google_drive.sheet`'

Access Denied: BigQuery BigQuery: No OAuth token with Google Drive scope was found.

解決策

一度tokenをrevokeした後、 --enable-gdrive-access を付けて、gcloud auth loginをすれば、Google DriveのScopeを含んで、ログインできる。

$ gcloud auth revoke
$ gcloud auth login --enable-gdrive-access

この時、先に gcloud auth revoke しないと、 以下のエラーになるので、注意

BigQuery: No OAuth token with Google Drive scope was found.