Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LocalでApplication Default Credentialsを利用している時にG Suite APIやFirebase APIを実行するとgoogleapi: Error 403: Request had insufficient authentication scopes. になる #124

Open
sinmetal opened this issue Apr 2, 2020 · 13 comments
Assignees

Comments

@sinmetal
Copy link
Member

sinmetal commented Apr 2, 2020

WHAT

Google SpreadsheetなどのG Suite系のAPIやFirebase APIをApplicationから実行しようとした時に、Application Default Credentialsを利用していると権限があるにも関わらず403でエラーになる。

Refs

WHY

gcloud auth application-default login の場合、GCPのScopeは最強のもので取得しているけど、その中にG SuiteやFirebaseは含まれていないからっぽい?

Solution

gcloud auth application-default login --scopes を利用して必要な任意のScopeを設定してやる。

Spreadsheetだと gcloud auth application-default login --scopes "https://www.googleapis.com/auth/spreadsheets" みたいな感じ

@sinmetal sinmetal self-assigned this Apr 2, 2020
@sinmetal sinmetal changed the title Local Application Default Credentialsでgoogleapi: Error 403: Request had insufficient authentication scopes. LocalでApplication Default Credentialsを利用している時にG Suite APIやFirebase APIを実行するとgoogleapi: Error 403: Request had insufficient authentication scopes. になる Apr 2, 2020
@sinmetal
Copy link
Member Author

sinmetal commented Apr 2, 2020

いっそのこと任意のService AccountのAccessTokenが欲しい場合 creating-short-lived-service-account-credentials が利用するのがよい。

これを実行する場合、対象のServiceAccountに対する token-creator のRoleが必要になると思うので、注意すること(具体的にどのRoleがあればいけるかはsinmetalはまだ試してない)

Go Sample Code

@apstndb
Copy link
Contributor

apstndb commented Apr 2, 2020

具体的にはこれですね
https://cloud.google.com/iam/docs/understanding-roles?hl=en#service-accounts-roles

roles/iam.serviceAccountTokenCreator

@apstndb
Copy link
Contributor

apstndb commented Apr 2, 2020

Spreadsheetだと gcloud auth application-default login --scopes "https://www.googleapis.com/auth/spreadsheets" みたいな感じ

だと本当に spreadsheets にしかアクセスできなくなるので、デフォルトで gcloud auth application-default login で付くスコープと合わせて

gcloud auth application-default login --scopes "https://www.googleapis.com/auth/spreadsheets,https://www.googleapis.com/auth/userinfo.email,https://www.googleapis.com/auth/cloud-platform,openid"

の方がよさそう

@sinmetal
Copy link
Member Author

sinmetal commented Apr 2, 2020

かっちょいい。FirebaseってどんなScopeしてるんだっけ?

@sinmetal
Copy link
Member Author

sinmetal commented Apr 2, 2020

https://www.googleapis.com/auth/firebase があれば、https://www.googleapis.com/auth/cloud-platform と同じノリでごそっと持てるのかな?

@apstndb
Copy link
Contributor

apstndb commented Apr 2, 2020

なお --scopes のデフォルト値は --scopes の説明に書いてある
https://cloud.google.com/sdk/gcloud/reference/auth/application-default/login#--scopes

By default https://www.googleapis.com/auth/userinfo.email, https://www.googleapis.com/auth/cloud-platform scopes are used. The list of possible scopes can be found at: https://developers.google.com/identity/protocols/googlescopes.

上に書いてある通り、スコープの一覧は
https://developers.google.com/identity/protocols/oauth2/scopes

@apstndb
Copy link
Contributor

apstndb commented Apr 2, 2020

https://www.googleapis.com/auth/firebase があれば、https://www.googleapis.com/auth/cloud-platform と同じノリでごそっと持てるのかな?

スコープの一覧にある通り、
https://www.googleapis.com/auth/firebase
が一番広そうですね。
https://www.googleapis.com/auth/firebase.readonly
もある

@sinmetal
Copy link
Member Author

sinmetal commented Apr 2, 2020

G Suiteはごそっと持つやつはなくて、強いて言えば https://developers.google.com/identity/protocols/oauth2/scopes#scriptv1 を見ながら、必要なやつをせっせと入れる感じかな?

@apstndb
Copy link
Contributor

apstndb commented Apr 2, 2020

そうなりそうですねえ

@sinmetal
Copy link
Member Author

sinmetal commented Apr 2, 2020

なるほどなー。まぁ、G Suite系はでかいし、色んなデータがあるから、致し方ないか

@apstndb
Copy link
Contributor

apstndb commented Apr 2, 2020

まあ神のアクセストークンを発行できるリフレッシュトークンがもしも漏れたらと考えるとこわいですしね…

@sinmetal
Copy link
Member Author

sinmetal commented Apr 2, 2020

#89 もちょい似てる話題だなと思ったので、メモ

@sinmetal
Copy link
Member Author

sinmetal commented Jan 5, 2021

Drive APIはQuotaProjectのオプションを指定しても、実行時にProjectNumber=764086051850 で固定され googleapi: Error 403: Access Not Configured. Drive API has not been used in project 764086051850 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/drive.googleapis.com/overview?project=764086051850 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry., accessNotConfigured と言われる。

この場合は gcloud auth application-default login --client-id-file を指定してやれば、ClientIDが存在するProjectをQuotaProjectとして実行することができる。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants