文系プログラマによるTIPSブログ

文系プログラマ脳の私が開発現場で学んだ事やプログラミングのTIPSをまとめています。

GAE/GOのデプロイ時のOAuth2認証後にlocalhostに遷移してしまう時の対処法

東京リージョンにGAE/GOをデプロイしようと思ったらこれだよ!
f:id:treeapps:20160521191008p:plain

前回の記事で東京リージョンのGCPプロジェクトを作ったので、早速GAE/GOをデプロイして速度確認しようとしたところ、ちょっと問題がありました。いつものように「goapp deloy」すると、以下のような事になりました。

tree:tree-maps-go tree$ goapp deploy
02:28 AM Application: tree-maps; version: go-echo
02:28 AM Host: appengine.google.com
02:28 AM Starting update of app: tree-maps, version: go-echo
02:28 AM Getting current resource limits.
Your browser has been opened to visit:

    https://accounts.google.com/o/oauth2/auth?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fappengine.admin+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcloud-platform+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2F&response_type=code&client_id=550516889912.apps.googleusercontent.com&access_type=offline

If your browser is on a different machine then exit and re-run this
application with the command-line parameter

  --noauth_local_webserver

こんな感じのコンソールがダラダラ流れてブラウザ起動してgoogleのoauth認証が始まるのですが、oauth認証の許可ボタンをクリックしたら、なんとlocalhost:8080に遷移してしまったではないですか!!

本来であれば、許可した後にverification codeが発行され、その値をコンソールに入力するとデプロイが続行しますよね。残念がらlocalhostに遷移してしまい、verification codeを取得する事ができませんでした。上のコンソールをよく見ると、「ブラウザで以下のURLを閲覧しろよ」の下のURLの「redirect_uri」パラメータの値が「localhost」になっているのです。

これ自体の原因は不明ですが、解決策は↑にヒントが書いてあり、以下のようにコマンドを実行すればlocalhostにならずにoauth認証され、正常にデプロイできるようになります。(ドットの部分はプロジェクトのパスを設定してもOKです)

tree:tree-maps-go tree$ appcfg.py update . --noauth_local_webserver
02:36 AM Application: tree-maps-152415; version: go-echo
02:36 AM Host: appengine.google.com
02:36 AM Starting update of app: tree-maps-152415, version: go-echo
02:36 AM Getting current resource limits.
Go to the following link in your browser:

    https://accounts.google.com/o/oauth2/auth?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fappengine.admin+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcloud-platform+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code&client_id=550516889912.apps.googleusercontent.com&access_type=offline

Enter verification code: hogehogehogehoge
Authentication successful.
02:37 AM Scanning files on local disk.
02:37 AM Cloning 1 static file.
02:37 AM Cloning 135 application files.
02:37 AM Uploading 81 files and blobs.
02:37 AM Uploaded 81 files and blobs.
02:37 AM Compilation starting.
02:37 AM Compilation: 108 files left.
02:37 AM Compilation completed.
02:37 AM Starting deployment.
02:37 AM Checking if deployment succeeded.
02:37 AM Deployment successful.
02:37 AM Checking if updated app version is serving.
02:38 AM Completed update of app: tree-maps-152415, version: go-echo

「--noauth_local_webserver」オプションですが、これはgoappコマンドには無く、appcfg.py側にあるオプションなので注意です。