Googoe App Engine、Go初学者です。
こちらを参考に勉強してます。
デプロイしたら
https://(プロジェクトID).appspot.com/
にて簡易なWebアプリにアクセスできるようになるサンプルです。
ですが、デプロイすると
oauthへのリンクが出力されます。
これはどういう事でしょうか?
app.yaml
yaml
application: point-viewer-335223 version: 1 runtime: go api_version: go1 # ルートのみ認証不要 handlers: - url: / script: _go_app - url: /.* script: _go_app login: required auth_fail_action: redirect
main.go
go
// // 会員向け点数告知(サンプル版) // package main import ( "fmt" "net/http" "strconv" "google.golang.org/appengine" "google.golang.org/appengine/datastore" "google.golang.org/appengine/user" ) // 会員情報 type UserInfo struct { UserId string // 会員ID Point int // 点数 } // 初期化 func init() { http.HandleFunc("/", rootPage) http.HandleFunc("/admin", adminPage) http.HandleFunc("/entry", entryAction) http.HandleFunc("/viewer", viewerPage) } // ルートページ(/) func rootPage(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-type", "text/html; charset=utf-8") ctx := appengine.NewContext(r) u := user.Current(ctx) // ログイン情報 var userView string if u == nil { url, _ := user.LoginURL(ctx, "/") // ログインURL userView = fmt.Sprintf("あなたは、ログインしていません。<a href='%s'>ログイン</a>", url) } else { url, _ := user.LogoutURL(ctx, "/") // ログアウトURL userView = fmt.Sprintf("あなたは、%sさんです。<a href='%s'>ログアウト</a>", u.Email, url) } fmt.Fprintf(w, ` <h1>会員向け点数告知(サンプル版)</h1> 会員向けにインターネットから自身の点数を確認させるWebサービスです。 Google(又はG Suite)ユーザ認証機能を利用しています。 %s <hr /> 以下のページはログインが必要です。 <a href="/admin">管理者ページ(会員と点数の登録)</a> <a href="/viewer">会員ページ(点数の照会)</a> `, userView) } // 管理者ページ(/admin) func adminPage(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-type", "text/html; charset=utf-8") //ctx := appengine.NewContext(r) fmt.Fprintf(w, ` <h1>管理者ページ</h1> ここでは管理者が会員と点数の登録を行います。 サンプル版では単純な上書き登録又は削除のみ可能です。 本来は管理者以外のアクセスを制限しますが、サンプル版では制限していません。 <hr /> <form action="/entry" method="post"> ■会員ID:<input type="input" name="user-id" /> Googleアカウント(又はG Suite)のメールアドレスを入力します。 ■点数:<input type="input" name="point" /> 点数を数字で入力します。(空欄:会員IDの削除) <button type="submit">登録</button </form> <hr /> <a href="/">戻る</a> `) } // 登録アクション(/entry) func entryAction(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-type", "text/html; charset=utf-8") ctx := appengine.NewContext(r) id := r.FormValue("user-id") // 会員ID if id == "" { http.Error(w, "会員IDが指定されていません。", http.StatusBadRequest) return } key := datastore.NewKey(ctx, "UserInfo", id, 0, nil) // KEY生成 pointStr := r.FormValue("point") // 点数 // 点数が未設定なら削除する if pointStr == "" { err := datastore.Delete(ctx, key) // 削除 if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } fmt.Fprintf(w, ` %s を削除しました。 <a href="/admin">戻る</a> `, id) return } // 点数が設定されていれば上書登録する point, err := strconv.Atoi(pointStr) //数値変換 if err != nil { http.Error(w, "数値変換エラー", http.StatusBadRequest) return } info := UserInfo{UserId: id, Point: point} _, err = datastore.Put(ctx, key) // 上書き登録 if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } fmt.Fprintf(w, ` 登録しました。 会員ID: %s 点数: %d点 <a href="/admin">戻る</a> `, info.UserId, info.Point) } // 会員ページ(/viewer) func viewerPage(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-type", "text/html; charset=utf-8") ctx := appengine.NewContext(r) u := user.Current(ctx) key := datastore.NewKey(ctx, "UserInfo", u.Email, 0, nil) // KEY生成 // 点数の参照 var info UserInfo err := datastore.Get(ctx, key, info) // 参照 if err != nil { http.Error(w, "あなたは会員登録されていません。", http.StatusNotFound) return } fmt.Fprintf(w, ` <h1>点数の照会</h1> こんにちは、%sさん あなたの点数は %d 点です。 <a href="/">戻る</a> `, info.UserId, info.Point) }
cmd
test9999@cloudshell:~/point-viewer (point-viewer-335223)$ goapp deploy 11:45 AM Application: point-viewer-335223; version: 1 11:45 AM Host: appengine.google.com 11:45 AM Starting update of app: point-viewer-335223, version: 1 11:45 AM Getting current resource limits. 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 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
###試したこと
リンクをクリックすると、添付画像のように許可画面に飛びます。
ここで許可にすると、遷移しますが、
このサイトにアクセスできませんlocalhost で接続が拒否されました。 次をお試しください 接続を確認する プロキシとファイアウォールを確認する ERR_CONNECTION_REFUSED
になります。
どうすれば
Webアプリを実行できるようになるでしょうか?
分かる方教えてくださると幸いです。
宜しくお願い致します。
まだ回答がついていません
会員登録して回答してみよう