- Retrieve access tokens for a Google social connection.
- Integrate with an AI agent to call Google APIs.
Pick your tech stack
Next.js
React SPA
Prerequisites
Before getting started, make sure you have completed the following steps:1
Create an Auth0 Account
To continue with this quickstart, you need to have an Auth0 account.
2
Create an Auth0 Application
Go to your Auth0 Dashboard to create a new Auth0 Application.
- Navigate to Applications > Applications in the left sidebar.
- Click the Create Application button in the top right.
- In the pop-up select Regular Web Applications and click Create.
- Once the Application is created, switch to the Settings tab.
- Scroll down to the Application URIs section.
- Set Allowed Callback URLs as:
http://localhost:3000/auth/callback - Set Allowed Logout URLs as:
http://localhost:3000 - Click Save in the bottom right to save your changes.
3
Enable Token Exchange Grant
Enable the Token Exchange Grant for your Auth0 Application. Go to Applications > [Your Application] > Settings > Advanced > Grant Types and enable the Token Exchange grant type.
4
Configure Google Social Integration
Set up a Google developer account that allows for third-party API calls by following the Google Social Integration instructions.
5
OpenAI Platform
Set up an OpenAI account and API key.
Prerequisites
Before getting started, make sure you have completed the following steps:- Create an Auth0 Account and a Dev Tenant
- Create and configure a Regular Web Application to use with this quickstart.
- Complete the User Authentication quickstart or download a starter template.
- Configure Google Social Connection (see Google Sign-in and Authorization) and select the Calender scope.
- Set up an OpenAI account and API key
Prepare Next.js app
Recommended: To use a starter template, clone the Auth0 AI samples repository:Install dependencies
In the root directory of your project, install the following dependencies:@auth0/ai-vercel: Auth0 AI SDK for Vercel AI built for GenAI applications powered by the Vercel AI SDK.ai: Core Vercel AI SDK module that interacts with various AI model providers.@ai-sdk/openai: OpenAI provider for the Vercel AI SDK.@ai-sdk/react: React UI components for the Vercel AI SDK.zod: TypeScript-first schema validation library.googleapis: Node.js client for Google APIs that supports authentication and authorization with OAuth 2.0.
Update the environment file
Copy the.env.example file to .env.local and update the variables with your Auth0 credentials. You can find your Auth0 domain, client ID, and client secret in the application you created in the Auth0 Dashboard.Get access tokens for other APIs
Use the Auth0 AI SDK to get access tokens for third-party APIs.Set up Token Vault for Google Social Connection
Setup Auth0 AI SDK for Google Social Connection. This will allow you to get access tokens for Google Social Connection using Token Vault.connection: pass in the name of the connection you want the user to sign up for/log into.refreshToken: pass in the function to get the refresh token from the current session.scopes: pass in the scopes for the service you want to get access to.
src/lib/auth0-ai.ts and instantiate a new Auth0 AI SDK client:src/lib/auth0-ai.ts
/src/lib/auth0.ts file with the following code:src/lib/auth0.ts
Use access token to call APIs from a tool
Once the user is authenticated, you can fetch an access token from the Token Vault using the Auth0 AI SDK. In this example, we fetch an access token for a Google social connection. Once you’ve obtained the access token for a social connection, you can use it with an AI agent to fetch data during a tool call and provide contextual data in its response.In our example, we create a file atsrc/lib/tools/google-calendar.ts. In the file, we define a tool, getCalendarEventsTool, that uses the access token with the Google Calendar API to query for calendar events in a specified date:src/lib/tools/google-calendar.ts
.env.local
Add step up authorization
When you try to use the tool, the application requests any additional Google scopes that are required but not yet authorized. This process is called step-up authorization.Let us implement step-up authorization.Install Auth0 AI Components for Next.js to get the required UI components.src/components/auth0-ai/FederatedConnections/FederatedConnectionInterruptHandler.tsx with the following code:src/components/auth0-ai/FederatedConnections/FederatedConnectionInterruptHandler.tsx
src/components/chat-window.tsx file to include the FederatedConnectionInterruptHandler component:src/components/chat-window.tsx
Add the tool to the AI agent
Handle the interrupts from the Agent and call the tool from your AI app to get calendar events. Update the/src/app/api/chat/route.ts file with the following code:src/app/api/chat/route.ts
Test your application
Start the application withnpm run dev. Then, navigate to http://localhost:3000. If you have already logged in, make sure to log out and log back in using Google. Then, ask your AI Agent a question about your calendar!That’s it! You successfully integrated external tool-calling into your project.Explore the example app on GitHub.Next steps
You have successfully added the ability to get access tokens for tool calling to your application. For next steps:- Call your APIs on user’s behalf docs.
- Learn more about Client-initiated account linking.
- Learn more about how Auth0’s Token Vault manages the tokens of supported identity providers.