Google APIsを利用し、OAuth2.0認証をしたあと、自分のアカウントのアカウントにアクセスできるサードパーティアプリにアプリが登録されない原因を知りたい

前提

自作しているWebアプリとGoogleカレンダーを連携し、現在のスケジュールの状態を表示したいと思っています。
自分なりに調べ、Node.jsとgoogleapisモジュールを使用し、それを実現しようとしております。

実現したいこと

発生している問題・エラーメッセージ

Google Cloud > APIとサービス > OAuth同意画面 で設定をし、そのあと
Google Cloud > APIとサービス > 認証情報 でOAuth 2.0 クライアント IDの認証情報を作成しました。
最初はhttps://github.com/googleapis/google-api-nodejs-client を参考にし、REDIRECT_URLには

http://localhost:3000/oauth2callback

を設定し、実行したところ、成功し
イメージ説明

↑の画像のように自分のアカウントのアカウントにアクセスできるサードパーティアプリに無事登録されていました。
ただ、
イメージ説明
↑この画面の「許可」を押した後に遷移する画面が

http://localhost:3000/oauth2callback

↑なのは使いづらいなと思い、連携したい自作のWebアプリに遷移させようと思い、REDIRECT_URLにWebアプリのURLを設定し、Google Cloud > APIとサービス > 認証情報 > 設定したウェブ アプリケーション のクライアント ID > 承認済みのリダイレクト URIにもWebアプリのURLを設定し、再度実行したところ、
イメージ説明
↑無事このような画面が出て「許可」を押したところ自作のWebアプリの画面に遷移しました。

しかし、
イメージ説明
↑この画面にはなにも追加されておらず、コードを実行しても毎回許可を求められてしまいます。(REDIRECT_URLにhttp://localhost:3000/oauth2callback を設定したときは1度許可すればそのあとは求められなかった。)

イメージ説明

↑Google Cloud > APIとサービス > OAuth同意画面 の公開ステータスがテストの時はそうなってしまうのか?
と思い色々調べていたのですが、明確な記載を見つけることができず、どなたか分かる方がいらっしゃいましたら教えていただけると助かります。
また、この情報がないと判断ができない等のご指摘もございましたら、お願いいたします。

該当のソースコード

Node.js

const http = require('http'); const { google } = require('googleapis'); const opn = require('opn'); const oauth2Client = new google.auth.OAuth2( YOUR_CLIENT_ID, YOUR_CLIENT_SECRET, YOUR_REDIRECT_URL ); // Access scopes for read-only Drive activity. const scopes = [ 'https://www.googleapis.com/auth/calendar.readonly' ]; // Generate a url that asks permissions for the Drive activity scope const authorizationUrl = oauth2Client.generateAuthUrl({ // 'online' (default) or 'offline' (gets refresh_token) access_type: 'offline', /** Pass in the scopes array defined above. * Alternatively, if only one scope is needed, you can pass a scope URL as a string */ scope: scopes, // Enable incremental authorization. Recommended as a best practice. include_granted_scopes: true }); const server = http.createServer(async (req, res) => { // acquire the code from the querystring, and close the web server. const qs = querystring.parse(url.parse(req.url).query); console.log(`Code is ${qs.code}`); res.end('Authentication successful! Please return to the console.'); server.close(); // Now that we have the code, use that to acquire tokens. const r = await oauth2Client.getToken(qs.code); // Make sure to set the credentials on the OAuth2 client. oauth2Client.setCredentials(r.tokens); console.info('Tokens acquired.'); console.log('oauth2Client', oauth2Client) // } }).listen(3000, () => { // open the browser to the authorize url to start the workflow opn(authorizationUrl); })

試したこと

Google Cloud > APIとサービス > OAuth同意画面 の公開ステータスを本番環境にしてみたが、結果は変わらなかった。
イメージ説明

補足情報(FW/ツールのバージョンなど)

Node.js v14.21.1

コメントを投稿

0 コメント