Go 1.11 はサポートが終了しており、2026 年 1 月 31 日に非推奨になります。非推奨になると、過去に組織のポリシーを使用して以前のランタイムのデプロイを再度有効にしていた場合でも、Go 1.11 アプリケーションをデプロイできなくなります。既存の Go 1.11 アプリケーションは、非推奨日以降も引き続き実行され、トラフィックを受信します。サポートされている最新バージョンの Go に移行することをおすすめします。
import("fmt""net/http""google.golang.org/appengine""google.golang.org/appengine/user")funcwelcome(whttp.ResponseWriter,r*http.Request){w.Header().Set("Content-type","text/html; charset=utf-8")ctx:=appengine.NewContext(r)u:=user.Current(ctx)ifu==nil{url,_:=user.LoginURL(ctx,"/")fmt.Fprintf(w,`<a href="%s">Sign in or register</a>`,url)return}url,_:=user.LogoutURL(ctx,"/")fmt.Fprintf(w,`Welcome, %s! (<a href="%s">sign out</a>)`,u,url)}
選択した認証オプションを使用してユーザーがアプリにログイン済みかどうかをアプリで検出できます。ユーザーがログインしていない場合は、Google アカウントでのログインまたは新しい Google アカウント作成のページに移動させることができます。ログインページの URL をアプリで取得するには、Users API のメソッドを呼び出します。認証を必要とするページをユーザーが訪問した際、アプリでこの URL をリンクとして表示するか、URL への HTTP リダイレクトを発行します。
Google アカウントまたは Google Workspace を認証に使用するアプリの場合、ユーザーがログインするときに、そのアプリケーションの名前がログインページに表示されます。表示される名前は、アプリケーションの登録時に指定したアプリケーション名です。この名前は、 Google Cloud コンソールの [認証情報] ページの [アプリケーション名] フィールドで変更できます。
ログインまたは Google アカウントの作成が完了すると、ユーザーは再びアプリケーションにリダイレクトされます。アプリは、ログイン URL を生成するメソッドにリダイレクト URL を渡します。
Users API には、アプリからログアウトするための URL を生成するメソッドがあります。このログアウト URL は、アプリに対するユーザーの認証を解除してから、元のアプリの URL にリダイレクトしますが、特に何かを表示することはありません。
ユーザーがアプリにログインした状態になるのは、アプリの画面の指示に従ってユーザーが自身のアカウントのメールアドレスとパスワードを入力した場合のみです。これは、ユーザーが自身の Google アカウントを使用して別のアプリにログインした場合にも該当します。
アカウント情報にアクセスする
ユーザーがアプリにログインした状態である間は、ユーザーがアプリに対してリクエストを行うたびにアプリからアカウントのメールアドレスにアクセスできます。アプリからユーザー ID にアクセスすることもできます。ユーザー ID はユーザーを一意に識別するものであり、これはユーザーがアカウントのメールアドレスを変更した場合であっても同様です。
アプリは現在のユーザーが管理者かどうかを特定することもできます。管理ユーザーとは、閲覧者、編集者、オーナー、App Engine アプリ管理者のロールが付与されているユーザーです。この機能を利用すると、アプリの管理用の機能を開発できます。他のユーザーは認証不要であってもかまいません。Go、Java、PHP、Python の API を利用して、URL を「管理者専用」に簡単に構成できます。
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["わかりにくい","hardToUnderstand","thumb-down"],["情報またはサンプルコードが不正確","incorrectInformationOrSampleCode","thumb-down"],["必要な情報 / サンプルがない","missingTheInformationSamplesINeed","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-09-04 UTC。"],[[["\u003cp\u003eThe Users API allows applications to check if a user is signed in, redirect them to sign-in pages, and request new Google account creation if needed.\u003c/p\u003e\n"],["\u003cp\u003eApplications can access a signed-in user's email address and determine if the user is an administrator, simplifying the creation of admin-only sections.\u003c/p\u003e\n"],["\u003cp\u003eAuthentication can be enforced at the URL level in the \u003ccode\u003eapp.yaml\u003c/code\u003e file, requiring users to be signed in or be administrators to access certain pages.\u003c/p\u003e\n"],["\u003cp\u003eOAuth can be used for user identification, offering an alternative to standard authentication methods, but pages requiring standard login will not load if only OAuth authentication is used.\u003c/p\u003e\n"],["\u003cp\u003eThe development server simulates Google Accounts with a fake sign-in, allowing any email address to be used, and providing a checkbox to indicate administrator status for testing.\u003c/p\u003e\n"]]],[],null,["# Users API for legacy bundled services\n\nThe Users API allows an application to:\n\n- Detect whether the current user has signed in.\n- Redirect the user to the appropriate sign-in page to sign in.\n- Request that your application user create a new Google account if they don't have one already.\n\n| This API is supported for first-generation runtimes and can be used when [upgrading to corresponding second-generation runtimes](/appengine/docs/standard/\n| go\n| /services/access). If you are updating to the App Engine Go 1.12+ runtime, refer to the [migration guide](/appengine/migration-center/standard/migrate-to-second-gen/go-differences) to learn about your migration options for legacy bundled services.\n\nWhile a user is signed in to the application, the app can access the user's\nemail address.\nThe app can also detect whether the current user is an\nadministrator (also called \"admin user\"),\nmaking it easy to implement admin-only areas of the app. To\nview the contents of the `user` package, see the\n[`user` package reference](/appengine/docs/legacy/standard/go111/users/reference).\n| **Note:** An *admin* user is any user that has the Viewer, Editor, Owner, or App Engine Admin [role](https://cloud.google.com/appengine/docs/legacy/standard/go111/roles). For more information on setting user roles, see [Roles that Grant Access to\n| App Engine](/appengine/docs/legacy/standard/go111/access-control).\n\nUser authentication in Go 1.11\n------------------------------\n\nThe following example greets a user who has signed in to the app with a\npersonalized message and a link to sign out. If the user is not signed in, the\napp offers a link to the sign-in page for Google Accounts. \n\n import (\n \t\"fmt\"\n \t\"net/http\"\n\n \t\"google.golang.org/appengine\"\n \t\"google.golang.org/appengine/user\"\n )\n\n func welcome(w http.ResponseWriter, r *http.Request) {\n \tw.Header().Set(\"Content-type\", \"text/html; charset=utf-8\")\n \tctx := appengine.NewContext(r)\n \tu := user.Current(ctx)\n \tif u == nil {\n \t\turl, _ := user.https://cloud.google.com/appengine/docs/legacy/standard/go111/reference/latest/user.html#google_golang_org_appengine_user_LoginURL(ctx, \"/\")\n \t\tfmt.Fprintf(w, `\u003ca href=\"%s\"\u003eSign in or register\u003c/a\u003e`, url)\n \t\treturn\n \t}\n \turl, _ := user.https://cloud.google.com/appengine/docs/legacy/standard/go111/reference/latest/user.html#google_golang_org_appengine_user_LogoutURL(ctx, \"/\")\n \tfmt.Fprintf(w, `Welcome, %s! (\u003ca href=\"%s\"\u003esign out\u003c/a\u003e)`, u, url)\n }\n\n### Enforcing sign in and admin access with app.yaml\n\nIf you have pages that require the user to be signed in order to\naccess, you can enforce this in your `app.yaml`\nfile.\n\n\u003cbr /\u003e\n\nThe handler configuration can also require that the user be a\nregistered administrator for the application; that is, the user must have the\nViewer, Editor, Owner, or App Engine Admin\n[role](https://cloud.google.com/appengine/docs/legacy/standard/go111/roles).\nThis makes it easy to build administrator-only sections of the site, without\nhaving to implement a separate authorization mechanism.\n\nTo learn how to configure authentication for URLs, see how to require login or\nadministrator status in the\n[`app.yaml` reference](/appengine/docs/legacy/standard/go111/config/appref#handlers_login).\n\nOAuth in Go\n-----------\n\nIn addition to the standard user authentication modes, users may be identified\nto your app via OAuth. OAuth is a protocol that allows a user to grant a third\nparty limited permission to access a web application on their behalf,\nwithout sharing their credentials (username and password) with the third\nparty. More information on the OAuth API, including the interaction required by\nclients, can be found in the [OAuth documentation](/appengine/docs/legacy/standard/go111/oauth).\n\nNote that using OAuth to identify your users is completely orthogonal to the\nstandard user authentication modes. For example, pages marked with\n`login: required` or `login: admin` will refuse to load if the user is only\nauthenticated via OAuth.\n\nHere is a simple example of accessing OAuth user information in a Go request\nhandler: \n\n import (\n \t\"fmt\"\n \t\"net/http\"\n\n \t\"google.golang.org/appengine\"\n \t\"google.golang.org/appengine/user\"\n )\n\n func welcomeOAuth(w http.ResponseWriter, r *http.Request) {\n \tctx := appengine.NewContext(r)\n \tu, err := user.CurrentOAuth(ctx, \"\")\n \tif err != nil {\n \t\thttp.Error(w, \"OAuth Authorization header required\", http.StatusUnauthorized)\n \t\treturn\n \t}\n \tif !u.Admin {\n \t\thttp.Error(w, \"Admin login only\", http.StatusUnauthorized)\n \t\treturn\n \t}\n \tfmt.Fprintf(w, `Welcome, admin user %s!`, u)\n }\n\nAuthentication options\n----------------------\n\nYour app can authenticate users using one of these options:\n\n- A Google Account\n- An account on your Google Workspace domain\n\n### Choosing an authentication option\n\nAfter you create your app, you can choose the authentication option you want to\nuse. By default, your app will use Google Accounts for authentication. To choose\nanother option, such as Google Workspace domain, go to the\n[settings](https://console.cloud.google.com/appengine/settings) page for your project\nin the Google Cloud console and click **Edit** . In the *Google authentication*\ndropdown menu, select the desired authentication type, and then click **Save**.\n\nSigning in and out\n------------------\n\nAn application can detect whether a user has signed in to the app with your\napp's chosen authentication option. If the user is not signed in, the app can\ndirect the user to Google Accounts to sign in or create a new Google account.\nThe app gets the URL for the sign-in page by calling a method of the Users API.\nThe app can display this URL as a link, or it can issue an HTTP redirect to the\nURL when the user visits a page that requires authentication.\n\nIf your app uses Google Accounts or Google Workspace for authentication,\nthe name of your application appears on the sign-in page when the user signs in\nto your application. The name shown is the application name that you specified\nwhen registering the application. You can change this name in the **Application\nname** field of the Google Cloud console\n[Credentials](https://console.cloud.google.com/apis/credentials/consent) page.\n\nOnce the user has signed in or created a Google account, the user is redirected\nback to your application. The app provides the redirect URL to the method that\ngenerates the sign-in URL.\n\nThe Users API includes a method to generate a URL for signing out of the app.\nThe sign-out URL de-authenticates the user from the app, then redirects back to\nthe app's URL without displaying anything.\n\nA user is not signed in to an application until they are prompted to do so by the\napp and enter their account's email address and password. This is true even if\nthe user has signed in to other applications using their Google Account.\n\nAccessing account information\n-----------------------------\n\nWhile a user is signed in to an app, the app can access the account's email\naddress for every request the user makes to the app. The app can also access a\nuser ID that identifies the user uniquely, even if the user changes the email\naddress for their account.\n\nThe app can also determine whether the current user is an administrator for the\napp. An *admin* user is any user that has the Viewer, Editor, Owner, or App\nEngine Admin\n[role](https://cloud.google.com/appengine/docs/legacy/standard/go111/roles).\nYou can use this feature to build administrative features for the app, even if\nyou don't authenticate other users. The Go, Java, PHP and Python APIs make it\neasy to configure URLs as \"administrator only\".\n| **Note:** Every user has the same user ID for all App Engine applications. If your app uses the user ID in public data, such as by including it in a URL parameter, you should use a hash algorithm with a \"salt\" value added to obscure the ID. Exposing raw IDs could allow someone to associate a user's activity in one app with that in another, or get the user's email address by coercing the user to sign in to another app.\n\nGoogle accounts and the development server\n------------------------------------------\n\nThe development server simulates the Google Accounts system using a fake\nsign-in screen. When your application calls the Users API to get the URL for the\nsign-in screen, the API returns a special development server URL that prompts\nfor an email address, but no password. You can type any email address into this\nprompt, and the app will behave as if you are signed in with an account with\nthat address.\n\nThe fake sign-in screen also includes a checkbox that indicates whether the\nfake account is an administrator; that is, whether the account has the\nViewer, Editor, Owner, or App Engine Admin\n[role](https://cloud.google.com/appengine/docs/legacy/standard/go111/roles). If\nyou check this box, the app will behave as if you are signed in using an\nadministrator account.\n\nSimilarly, the Users API returns a sign-out URL that cancels the fake sign-in."]]