|
| 1 | +--- |
| 2 | +title: Use the Google Auth Platform to authenticate users |
| 3 | +slug: /develop/tutorials/authentication/google |
| 4 | +description: Learn how to authenticate users with Google's OpenID Connect (OIDC) service |
| 5 | +--- |
| 6 | + |
| 7 | +# Use the Google Auth Platform to authenticate users |
| 8 | + |
| 9 | +Google is one of the most popular identity providers for social logins. You can use the Google Auth Platform with both private and organizational Google accounts. This tutorial configures authentication for anyone with a Google account. For more information, see Google's overview of the [Google Auth Platform](https://support.google.com/cloud/topic/15540269?hl=en&ref_topic=3473162&sjid=576431444945556851-NC) and [OpenID Connect](https://developers.google.com/identity/openid-connect/openid-connect#discovery). |
| 10 | + |
| 11 | +## Prerequisites |
| 12 | + |
| 13 | +- This tutorial requires the following Python libraries: |
| 14 | + |
| 15 | + ```text |
| 16 | + streamlit>=1.42.0 |
| 17 | + Authlib>=1.3.2 |
| 18 | + ``` |
| 19 | + |
| 20 | +- You should have a clean working directory called `your-repository`. |
| 21 | +- You must have a Google account and accept the terms of [Google Cloud](https://console.cloud.google.com/) to use their authentication service. |
| 22 | +- You must have a project in Google Cloud within which to create your application. |
| 23 | + For more information about managing your projects in Google Cloud, see [Creating and managing projects](https://cloud.google.com/resource-manager/docs/creating-managing-projects) in Google's documentation. |
| 24 | + |
| 25 | +## Summary |
| 26 | + |
| 27 | +In this tutorial, you'll build an app that users can log in to with their Google accounts. When they log in, they'll see a personalized greeting with their name and have the option to log out. |
| 28 | + |
| 29 | +Here's a look at what you'll build: |
| 30 | + |
| 31 | +<Collapse title="Complete code" expanded={false}> |
| 32 | + |
| 33 | +`.streamlit/secrets.toml` |
| 34 | + |
| 35 | +```toml |
| 36 | +[auth] |
| 37 | +redirect_uri = "http://localhost:8501/oauth2callback" |
| 38 | +cookie_secret = "xxx" |
| 39 | +client_id = "xxx" |
| 40 | +client_secret = "xxx" |
| 41 | +server_metadata_url = ( |
| 42 | + "https://accounts.google.com/.well-known/openid-configuration" |
| 43 | +) |
| 44 | +``` |
| 45 | + |
| 46 | +`app.py` |
| 47 | + |
| 48 | +```python |
| 49 | +import streamlit as st |
| 50 | + |
| 51 | +def login_screen(): |
| 52 | + st.header("This app is private.") |
| 53 | + st.subheader("Please log in.") |
| 54 | + st.button("Log in with Google", on_click=st.login) |
| 55 | + |
| 56 | +if not st.experimental_user.is_logged_in: |
| 57 | + login_screen() |
| 58 | +else: |
| 59 | + st.header(f"Welcome, {st.experimental_user.name}!") |
| 60 | + st.button("Log out", on_click=st.logout) |
| 61 | +``` |
| 62 | + |
| 63 | +</Collapse> |
| 64 | + |
| 65 | +## Create a web application in Google Cloud Console |
| 66 | + |
| 67 | +In this section, you'll complete three steps to create your web application in your project in Google Cloud Console: |
| 68 | + |
| 69 | +- Configure your consent screen. |
| 70 | +- Configure your audience. |
| 71 | +- Configure your client. |
| 72 | + |
| 73 | +The consent screen is what users see from Google within the authentication flow. The audience settings manage your application's status (_Testing_ or _Published_). Creating a client for your web application generates the ID and secrets needed to configure your Streamlit app. To learn more about consent screens, audience, and clients, see Google's overview of the [Google Auth Platform](https://support.google.com/cloud/topic/15540269?hl=en&ref_topic=3473162&sjid=576431444945556851-NC). |
| 74 | + |
| 75 | +### Configure your consent screen |
| 76 | + |
| 77 | +1. Go to the [Google Auth Platform](https://console.cloud.google.com/auth/overview), and sign in to Google. |
| 78 | + |
| 79 | +1. In the upper-left corner, select your project. |
| 80 | + |
| 81 | +1. In the left navigation menu, select "**Branding**." |
| 82 | + |
| 83 | +1. Fill in the required information for your application's consent screen. |
| 84 | + |
| 85 | + This information controls what users see within the Google authentication flow. Your "**App name**" is displayed to users within Google's prompts. Google asks users to consent to sending their account information to your application. If you are developing locally and/or deploying on Streamlit Community Cloud, in "**Authorized domain**," use `example.com`. For more information about the available fields, see [Setting up your OAuth consent screen](https://support.google.com/cloud/answer/10311615). |
| 86 | + |
| 87 | +1. At the bottom of the branding page, select "**SAVE**." |
| 88 | + |
| 89 | +### Configure your audience |
| 90 | + |
| 91 | +1. In the left navigation menu, select "**Audience**." |
| 92 | + |
| 93 | +1. Below "OAuth user cap" → "Test users," select "**ADD USERS**." |
| 94 | + |
| 95 | +1. Enter the email address for a personal Google account, and select "**SAVE**." |
| 96 | + |
| 97 | + When you create a new application in the Google Auth Platform, its status is _Testing_. While the status is _Testing_, only specific users can authenticate to your application; users can't register themselves. Therefore, add any email address you want to use for testing your app in development. When you're ready to publish your app, you'll return to this section and change the status to _Published_. After an application is published, your application will accept new users. |
| 98 | + |
| 99 | +### Configure your client |
| 100 | + |
| 101 | +1. In the left navigation menu, select "**Clients**." |
| 102 | + |
| 103 | +1. At the top of the client list, select "**CREATE CLIENT**." |
| 104 | + |
| 105 | +1. For the application type, select "**Web application**." |
| 106 | + |
| 107 | +1. Enter a unique name for your application. |
| 108 | + |
| 109 | + The client name is used internally and not shown to your users. |
| 110 | + |
| 111 | +1. Skip over "Authorized JavaScript origins." |
| 112 | + |
| 113 | +1. Under "Authorized redirect URIs," select "**ADD URI**." |
| 114 | + |
| 115 | +1. Enter your app's URL with the pathname `oauth2callback`. |
| 116 | + |
| 117 | + For example, if you are developing locally, enter `http://localhost:8501/oauth2callback`. If you are using a different port, change `8501` to match your port. |
| 118 | + |
| 119 | +1. Optional: Add additional authorized redirect URIs. |
| 120 | + |
| 121 | + If you will host your app from multiple URLs, or if you know a URL you will use in the future, you can add it now. Ensure that each URL includes the `oauth2callback` pathname. |
| 122 | + |
| 123 | +1. At the bottom of the screen, select "**CREATE**." |
| 124 | + |
| 125 | +You now have a client in Google Cloud that's ready to authenticate your users. |
| 126 | + |
| 127 | +### Gather your application's details |
| 128 | + |
| 129 | +1. From the clients page, select your new client. |
| 130 | + |
| 131 | +1. To store your app information to use in later steps, open a text editor, or (even better) create a new item in a password locker. |
| 132 | + |
| 133 | + Always handle your app secrets securely. Remember to label the values as you paste them so you don't mix them up. |
| 134 | + |
| 135 | +1. On the right, copy your "Client ID" and "Client secret" into your text editor. |
| 136 | + |
| 137 | +For the Google Auth Platform, the server metadata URL is shared between all applications and isn't listed individually in your client. The server metadata URL for the Google Auth Platform is `https://accounts.google.com/.well-known/openid-configuration`. For more information about the server metadata URL, see [The discovery document](https://developers.google.com/identity/openid-connect/openid-connect#discovery) in Google's documentation. |
| 138 | + |
| 139 | +## Build the example |
| 140 | + |
| 141 | +To create an app with user authentication, you'll need to configure your secrets and prompt your users to log in. You'll use secrets management to store the information from your client, and then create a simple app that welcomes your user by name after they log in. |
| 142 | + |
| 143 | +### Configure your secrets |
| 144 | + |
| 145 | +1. In `your_repository`, create a `.streamlit/secrets.toml` file. |
| 146 | + |
| 147 | +1. Add `secrets.toml` to your `.gitignore` file. |
| 148 | + |
| 149 | + <Important> |
| 150 | + Never commit secrets to your repository. For more information about `.gitignore`, see [Ignoring files](https://docs.github.com/en/get-started/getting-started-with-git/ignoring-files). |
| 151 | + </Important> |
| 152 | + |
| 153 | +1. Generate a strong, random secret to use as your cookie secret. |
| 154 | + |
| 155 | + The cookie secret is used to sign each user's identity cookie, which Streamlit stores when they log in. |
| 156 | + |
| 157 | +1. In `.streamlit/secrets.toml`, add your connection configuration: |
| 158 | + |
| 159 | + ```toml |
| 160 | + [auth] |
| 161 | + redirect_uri = "http://localhost:8501/oauth2callback" |
| 162 | + cookie_secret = "xxx" |
| 163 | + client_id = "xxx" |
| 164 | + client_secret = "xxx" |
| 165 | + server_metadata_url = ( |
| 166 | + "https://accounts.google.com/.well-known/openid-configuration" |
| 167 | + ) |
| 168 | + ``` |
| 169 | + |
| 170 | + Replace the values of `client_id` and `client_secret` with the values you copied into your text editor earlier. Replace the value of `cookie_secret` with the random secret you generated in the previous step. |
| 171 | + |
| 172 | +1. Save your `secrets.toml` file. |
| 173 | + |
| 174 | +### Initialize your app |
| 175 | + |
| 176 | +1. In `your_repository`, create a file named `app.py`. |
| 177 | +1. In a terminal, change directories to `your_repository`, and start your app: |
| 178 | + |
| 179 | + ```bash |
| 180 | + streamlit run app.py |
| 181 | + ``` |
| 182 | + |
| 183 | + Your app will be blank because you still need to add code. |
| 184 | + |
| 185 | +1. In `app.py`, write the following: |
| 186 | + |
| 187 | + ```python |
| 188 | + import streamlit as st |
| 189 | + ``` |
| 190 | + |
| 191 | +1. Save your `app.py` file, and view your running app. |
| 192 | +1. In your app, select "**Always rerun**", or press the "**A**" key. |
| 193 | + |
| 194 | + Your preview will be blank but will automatically update as you save changes to `app.py`. |
| 195 | + |
| 196 | +1. Return to your code. |
| 197 | + |
| 198 | +### Log the user in and out |
| 199 | + |
| 200 | +1. Define a function that prompts the user to log in: |
| 201 | + |
| 202 | + ```python |
| 203 | + def login_screen(): |
| 204 | + st.header("This app is private.") |
| 205 | + st.subheader("Please log in.") |
| 206 | + st.button("Log in with Google", on_click=st.login) |
| 207 | + ``` |
| 208 | + |
| 209 | + This function displays a short message and a button. Streamlit's login command is assigned to the button as a callback. |
| 210 | + |
| 211 | + <Note> |
| 212 | + If you don't want to use a callback, you can replace the last line with an equivalent `if` statement: |
| 213 | + ```diff |
| 214 | + - st.button("Log in with Google", on_click=st.login) |
| 215 | + + if st.button("Log in with Google"): |
| 216 | + + st.login() |
| 217 | + ``` |
| 218 | + </Note> |
| 219 | + |
| 220 | +1. Conditioned on whether the user is logged in, call your function to prompt the user, or show their information: |
| 221 | + |
| 222 | + ```python |
| 223 | + if not st.experimental_user.is_logged_in: |
| 224 | + login_screen() |
| 225 | + else: |
| 226 | + st.experimental_user |
| 227 | + ``` |
| 228 | + |
| 229 | + Because `st.experimental_user` is a dict-like object in a line by itself, Streamlit magic displays it in your app. |
| 230 | + |
| 231 | +1. Save your `app.py` file, and test your running app. |
| 232 | + |
| 233 | + In your live preview, when you log in to your app, the login button is replaced with the contents of your identity token. Observe the different values that are available from Google. You can use these values to personalize your app for your users. |
| 234 | + |
| 235 | +1. Return to your code. |
| 236 | + |
| 237 | +1. Replace `st.experimental_user` with a personalized greeting: |
| 238 | + |
| 239 | + ```diff |
| 240 | + else: |
| 241 | + - st.experimental_user |
| 242 | + + st.header(f"Welcome, {st.experimental_user.name}!") |
| 243 | + ``` |
| 244 | + |
| 245 | +1. Add a logout button: |
| 246 | + |
| 247 | + ```python |
| 248 | + st.button("Log out", on_click=st.logout) |
| 249 | + ``` |
| 250 | + |
| 251 | +1. Save your `app.py` file and test your running app. |
| 252 | + |
| 253 | + In your live preview, if you log out of your app, it will return to the login prompt. |
| 254 | + |
| 255 | +## Deploy your app on Community Cloud |
| 256 | + |
| 257 | +When you are ready to deploy your app, you must update your application on Google Cloud and your secrets. The following steps describe how to deploy your app on Community Cloud. |
| 258 | + |
| 259 | +1. Add a `requirements.txt` file to your repository with the following lines: |
| 260 | + |
| 261 | + ```txt |
| 262 | + streamlit>=1.42.0 |
| 263 | + Authlib>=1.3.2 |
| 264 | + ``` |
| 265 | + |
| 266 | + This ensures that the correct Python dependencies are installed for your deployed app. |
| 267 | + |
| 268 | +1. Save your `requirements.txt` file. |
| 269 | + |
| 270 | +1. Deploy your app, and copy your app's URL into your text editor. |
| 271 | + |
| 272 | + You'll use your app's URL to update your secrets and client configuration in the following steps. For more information about deploying an app on Community Cloud, see [Deploy your app](/deploy/streamlit-community-cloud/deploy-your-app). |
| 273 | + |
| 274 | +1. In your [app settings](/deploy/streamlit-community-cloud/manage-your-app/app-settings) in Community Cloud, select "**Secrets**." |
| 275 | + |
| 276 | +1. Copy the contents of your local `secrets.toml` file, and paste them into your app settings. |
| 277 | + |
| 278 | +1. Change your `redirect_uri` to reflect your deployed app's URL, which you copied earlier in this tutorial. |
| 279 | + |
| 280 | + For example, if your app is `my_streamlit_app.streamlit.io`, your redirect URI would be `https://my_streamlit_app.streamlit.io/oauth2callback`. |
| 281 | + |
| 282 | +1. Save and close your settings. |
| 283 | + |
| 284 | +1. Return to the clients page in the Google Auth Platform, and select your client. |
| 285 | + |
| 286 | +1. Under "Authorized redirect URIs," add or update a URI to match your new `redirect_uri`. |
| 287 | + |
| 288 | +1. At the bottom of the page, select "**SAVE**." |
| 289 | + |
| 290 | +1. Open your deployed app, and test it. |
| 291 | + |
| 292 | + Your Google Cloud application's status is still _Testing_. You should be able to log in and out of your app with the personal Google account you entered on the "Audience" page. |
| 293 | + |
| 294 | +1. When you are ready for others to use your app, return to the "Audience" page in the Google Auth Platform, and set your application status to _Published_. |
0 commit comments