Skip to content

Sign in with Google

Your users sign in to your app with their Google account. Your app receives an ID token from Google and exchanges it for an ObjectsGrid access token, which it then sends with every API request. ObjectsGrid creates the user's User Account on their first sign-in.

This guide takes you through it in four steps:

  1. Register your app with Google and get its client id.
  2. Add the Sign in with Google button to your app.
  3. Register the client id with your Organization Account.
  4. Exchange the ID token for an access token.
sequenceDiagram
    participant User
    participant App as Your app
    participant Google
    participant OG as ObjectsGrid
    User->>App: Clicks "Sign in with Google"
    App->>Google: Sign-in request with your client id
    Google-->>App: ID token
    App->>OG: POST /oauth2/token with the ID token
    OG-->>App: Access token and refresh token
    App->>OG: API requests with Authorization: Bearer <access token>

1. Register your app with Google

You need a Google Cloud account. It's best to create a Google Cloud project for your app: in the Google Cloud console, open the project list at the top left and choose New project.

Start the Google Auth Platform. Open Google Auth Platform and click Get started.

Google Auth Platform overview with the Get started button

App information. Enter your app's name and the support email address your users can write to, then click Next.

App information form with app name and user support email

Audience. Choose External, so that any Google user can sign in, and click Next.

Audience step with External selected

Contact information. Enter the email address Google should use to contact you, and click Next.

Contact information step

Finish. Accept the Google API Services User Data Policy, click Continue, then Create.

Agreement to the Google API Services User Data Policy

Create button of the Google Auth Platform setup

Confirmation that the OAuth configuration was created

Branding. Enter your app's branding, such as its logo and home page, and click Save.

Branding page of the Google Auth Platform

Audience page. Leave it as it is for now. When sign-in works, come back here to publish your app.

Audience page with the app in testing

Create a client. In Clients, click Create client.

Clients page with the Create client button

Choose the application type Web application and fill in:

  • Authorized JavaScript origins: your app's origin, such as https://app.example.com.
  • Authorized redirect URIs: the page that shows the sign-in button, often your home or login page.

Click Create.

Client details with authorized JavaScript origins and redirect URIs

Copy the client id. Google shows the client id and a client secret. You only need the client id, which ends in .apps.googleusercontent.com.

Created OAuth client showing the client id

Leave the Data Access page unchanged: sign-in needs no extra scopes. The Verification Center then shows Verification not required.

Verification Center showing Verification not required

2. Add the button to your app

Google's Sign in with Google for Web library draws the button and hands your page the ID token. A minimal page, like the ObjectsGrid test token page:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Sign in</title>
  <script src="https://accounts.google.com/gsi/client" async></script>
  <script>
    function onGoogleSignIn(response) {
      // response.credential is the Google ID token.
      exchangeForAccessToken(response.credential);
    }
  </script>
</head>
<body>
  <div id="g_id_onload"
       data-client_id="<your client id>.apps.googleusercontent.com"
       data-context="signin"
       data-ux_mode="popup"
       data-callback="onGoogleSignIn"
       data-auto_prompt="false">
  </div>
  <div class="g_id_signin" data-type="standard" data-text="signin_with"></div>
</body>
</html>

The two div elements come from Google's library: the first configures it with your client id, the second is the button.

The two Google Identity Services elements in the page

When the user signs in, Google calls your callback. The ID token is response.credential:

The credential field of Google's response holds the ID token

3. Register the client id with ObjectsGrid

ObjectsGrid accepts an ID token only if it was issued to an app your Organization Account lists in its identityProviders, so other websites can't sign your users in to your account. Your Organization Account starts with the app you registered with; add your own app's client id.

As an administrator, read your Organization Account, add your client id to the Google entry of identityProviders, and replace it:

curl https://apis.objectsgrid.com/organizationaccounts/<organization account id> \
  -H 'X-ObjectsGrid-AccessPoint: EU' \
  -H "Authorization: Bearer $TOKEN"
curl -X PUT https://apis.objectsgrid.com/organizationaccounts/<organization account id> \
  -H 'Content-Type: application/json' \
  -H 'X-ObjectsGrid-AccessPoint: EU' \
  -H "Authorization: Bearer $TOKEN" \
  -d '{
        "name": "Northpeak Outdoor",
        "accessPoint": "EU",
        "identityProviders": [
          {
            "issuer": "https://accounts.google.com",
            "clientIds": ["<client id you registered with>", "<your client id>.apps.googleusercontent.com"]
          }
        ]
      }'

Send the whole account you read, with your change. Keep the client ids already listed unless you want to stop them from signing in.

4. Exchange the ID token for an access token

Your app sends the ID token to the token endpoint, as a JSON object or form-encoded. client_id is your Organization Account id:

async function exchangeForAccessToken(idToken) {
  const response = await fetch("https://apis.objectsgrid.com/oauth2/token", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      "X-ObjectsGrid-AccessPoint": "EU",
    },
    body: JSON.stringify({
      grant_type: "urn:ietf:params:oauth:grant-type:token-exchange",
      subject_token: idToken,
      subject_token_type: "urn:ietf:params:oauth:token-type:id_token",
      client_id: "<organization account id>",
    }),
  });
  const { access_token, expires_in, refresh_token } = await response.json();
  // Send access_token with every API request; use refresh_token before it expires.
}

The response is 200 with an access token for one hour and a refresh token for three months. If you get 400 with "error": "invalid_grant", check that the client id is in your identityProviders and that the ID token hasn't expired. See Authentication for refreshing and revoking tokens.

Publish your app

While your Google app is in testing, only the test users you list can sign in. When sign-in works, go to the Audience page of the Google Auth Platform and publish the app.