LoginSignup
0
0

More than 1 year has passed since last update.

GASでoAuth2で接続しようとしているときに「Grant type "authorization_code" not supported」が出たときにやったこと。

Posted at

GASでoAuth2で接続しようとしているときに「Grant type "authorization_code" not supported」が出たときにやったこと。

概要

https://github.com/googleworkspace/apps-script-oauth2
を使ってoAuthで外部アプリケーションと接続していた。久しぶりに動かしてみたら「Grant type "authorization_code" not supported」のエラーが出て動かない。これを治していこう。

前提条件

パラメータ名 有効な値
client_id アプリケーション詳細画面で確認できるクライアントID
client_secret アプリケーション詳細画面に表示されているシークレット文字列
code 取得した認可コード文字列
grant_type “client_credentials”を指定

結論

トークン取得時にclient_credentialsが指定できていなかった。
ソースから

Service_.prototype.handleCallback = function(callbackRequest) {
...略
  var payload = {
    code: code,
    client_id: this.clientId_,
    client_secret: this.clientSecret_,
    redirect_uri: this.getRedirectUri(),
    grant_type: 'authorization_code'
  };

となってコールバック処理時のデフォルトgrant_typeが'authorization_code'であることがわかる。このauthorization_codeが、このまま流れている。これを治すには、

const auth = OAuth2.createService('tetete')
auth.setGrantType('client_credentials')

などでは治らない。

  auth.setTokenPayloadHandler(function(tokenPayload) {
       tokenPayload.grant_type = "client_credentials";
        return tokenPayload;
      })

のように、コールバック処理中にpayloadを捕まえて変形させる必要があった。

つまりsetTokenPayloadHandlerを使えば良い。

おつかれさまでした。

0
0
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
0
0