- Retrieve access tokens for a Google social connection.
- Integrate with an AI agent to call Google APIs.
Pick your tech stack
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.
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-langchain
: Auth0 AI SDK for LangChain built for GenAI applications powered by LangChain.@langchain/langgraph
: For building stateful, multi-actor applications with LLMs.langchain
: The LangChain library.@langchain/core
: LangChain core libraries.@langchain/openai
: OpenAI provider for LangChain.@langchain/community
: LangChain community integrations.langgraph-nextjs-api-passthrough
: API passthrough for LangGraph.
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 others APIs
Use the Auth0 AI SDK for LangChain to get access tokens for third-party APIs.Set up Token Vault for Google social connection
Set up the Auth0 AI SDK for a Google Social Connection. This allows you to get access tokens for a Google social connection using Token Vault:connection
: pass in the name of the connection you want the user to sign up for/log into.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
Pass credentials to the tools
Update the/src/lib/auth0.ts
file with the following code:src/lib/auth0.ts
/src/app/api/chat/[..._path]/route.ts
file with the following code. The refreshToken
will be passed to your LangGraph agent so we can use it from the Auth0 AI SDK to get Google access tokens from the server.src/app/api/chat/[..._path]/route.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 connection, you can use it with an AI agent to fetch data during a tool call and provide contextual data in its response.In this example, we will use theGmailSearch
from the @langchain/community
tools. This tool will use the access token provided by Token Vault to query for emails.src/lib/agent.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 the 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
Test your application
Start the application withnpm run all:dev
. Then, navigate to http://localhost:3000
.This will open the LangGraph Studio in a new tab. You can close it as we won’t
require it for testing the application.
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.