Skip to main content

Accounts (Google)

Connect Account

This API endpoint is triggered via Google redirect URL after a user successfully provisions their OAuth credentials. Please read OAuth concept for further details.

Endpoint

GET /google/accounts/connect HTTP/1.1
Host: api.atomcal.com/api/v2
Content-Type: application/x-www-form-urlencoded
Triggered by: Google OAuth

Using the provision token by google, it requests users access_token and further requests user details using the access_token and store them for later use.

Example Responses

200 (successful operation)

res.send(`
  <script>
    window.close();
  </script>
`);

Authorization URL is triggered in a popup window, and it is closed on success

400 (bad response)

res.send(`
    <body>
        Something went wrong; to fix this please disconnect Google completely then retry.

        Follow this guide <a href="https://scrims.xyz/faqs/how-to-fix-google-calendar-connection" target="_blank">https://scrims.xyz/faqs/how-to-fix-google-calendar-connection</a>
    </body>
`);

Implementation details

Use a Google Authentication Code to Generate a Google Auth Token URL

To obtain an access token and refresh token from Google using the authorization code:

  1. Generate the Authorization URL:
https://accounts.google.com/o/oauth2/v2/auth?response_type=code&client_id=YOUR_CLIENT_ID&redirect_uri=YOUR_REDIRECT_URI&scope=YOUR_SCOPES&state=STATE_PARAMETER&access_type=offline&prompt=consent
  1. Handle the Authorization Response:
https://yourapp.com/oauth2/callback?code=AUTHORIZATION_CODE&state=xyz
  1. Exchange the Authorization Code for an Access Token:
https://oauth2.googleapis.com/token
POST /token HTTP/1.1
Host: oauth2.googleapis.com
Content-Type: application/x-www-form-urlencoded

code=AUTHORIZATION_CODE&
client_id=YOUR_CLIENT_ID&
client_secret=YOUR_CLIENT_SECRET&
redirect_uri=YOUR_REDIRECT_URI&
grant_type=authorization_code
{
  "access_token": "ACCESS_TOKEN",
  "expires_in": 3600,
  "refresh_token": "REFRESH_TOKEN",
  "scope": "https://www.googleapis.com/auth/userinfo.profile",
  "token_type": "Bearer"
}

Store Fetched Auth Data (Including Refresh Token)

To store the obtained authentication data for later use:

  1. Save Tokens Securely:
  • Store the access_token, refresh_token, and any other relevant information in a secure database or storage system.
  • Ensure tokens are encrypted and stored securely to prevent unauthorized access.

Use Google User Info URL To Request and Store Basic User Info

To fetch user information using the access token:

  1. Fetch User Information:
  • Make a GET request to the Google UserInfo endpoint using the access token.
https://www.googleapis.com/oauth2/v3/userinfo
GET /oauth2/v3/userinfo HTTP/1.1
Host: www.googleapis.com
Authorization: Bearer ACCESS_TOKEN
  1. Handle the User Information Response:
  • Parse the response to extract basic user information.
{
  "sub": "1234567890",
  "name": "John Doe",
  "given_name": "John",
  "family_name": "Doe",
  "picture": "https://lh3.googleusercontent.com/a-/AOh14GhjKZXzx5QJ1YX-aBcHI9Jds4Zz3_kLQO_qedhv",
  "email": "[email protected]",
  "email_verified": true,
  "locale": "en"
}
  1. Store User Information:
  • Save the fetched user information in your database for later use.
  • Ensure data privacy and protection by following best practices for secure data storage

Edge cases

If refresh token is not found, throws an error and asks user to disconnect Google account completely using a help center guide.