Opening Problem Statement
Meet Sarah, a developer managing multiple Facebook apps that integrate deeply with her company’s marketing platform. She spends hours every few weeks manually generating long-lived Facebook user and page access tokens to keep the apps running smoothly. Each manual process is prone to errors — a mistyped client secret or expired token means lost functionality, delivery delays, and frustrated clients. Sarah knows this repetitive chore diverts her focus from feature development and costs her team precious engineering hours.
Imagine needing to exchange short-lived user tokens for long-lived tokens regularly to maintain reliable API access without disruptions. Despite Facebook’s clear documentation, the token exchange requires several authenticated HTTP requests with precise parameters. Doing this manually or with partial scripts can lead to mistakes, wasted time, and potential security risks from mishandling credentials.
This is the exact challenge this n8n workflow solves — automating the entire process of fetching long-lived user and page access tokens from Facebook, freeing Sarah from repetitive manual steps while tightening security and ensuring tokens are always fresh.
What This Automation Does
This n8n workflow automates the process of retrieving long-lived Facebook access tokens for both user and page accounts. When you trigger the workflow, it:
- Accepts your Facebook App credentials and a short-lived user access token as inputs.
- Makes an HTTP request to Facebook’s OAuth endpoint to exchange the short-lived token for a long-lived user token.
- Uses the long-lived user token to request long-lived page access tokens for pages the user manages.
- Outputs both user and page tokens, ready for use in your applications or integrations.
- Reduces manual token refresh efforts, saving hours of repetitive work.
- Improves reliability by structured token exchanges with proper error handling and parameter mapping.
Prerequisites ⚙️
- n8n account or self-hosted n8n instance 🔑
- Facebook Developer account with an app and credentials (Client ID and Client Secret) 🔐
- Initial short-lived Facebook user access token obtained via Facebook Login 📧
- Basic knowledge of n8n interface for configuring nodes
- Optional: Self-hosting provider like Hostinger if you prefer full control
Step-by-Step Guide
1. Start Your Workflow with a Manual Trigger
In n8n, create a new workflow. Add the Manual Trigger node named “When clicking ‘Test workflow’”. This node allows you to initiate the workflow manually to test and run the token exchange process on demand.
Navigation: Click + Add node → Search and select Manual Trigger node.
Outcome: A manual trigger point to start your workflow anytime.
2. Set the Required Facebook Parameters
Add a Set node named “Set Parameter” right after the manual trigger. Here you will define four key parameters that your workflow needs:
- client_id: Your Facebook App ID
- client_secret: Your Facebook App Secret
- user_access_token: The short-lived Facebook user token you want to exchange
- app_scoped_user_id: (Optional) The user ID scoped to the app to list pages
How to add: Click the Set Parameter node → Go to the Values to Set section → Add these exact fields and use your actual values or credentials.
Common Mistake: Avoid extra spaces or typos in parameter keys like ” client_id” instead of “client_id” which could cause failed API calls.
3. Exchange Short-Lived Token for Long-Lived User Token
Add an HTTP Request node named “Get Long Lived FB User Token”. This node will make a GET request to Facebook’s OAuth endpoint to exchange the short-lived token for a long-lived token.
Configuration:
- Method: GET
- URL: https://graph.facebook.com/v20.0/oauth/access_token
- Query Parameters:
- grant_type = fb_exchange_token
- client_id =
{{$json["client_id"]}} - client_secret =
{{$json["client_secret"]}} - fb_exchange_token =
{{$json.user_access_token}}
These parameters dynamically reference values set in the previous node.
Expected result: The response body includes the long-lived user access token.
Common issues: Incorrect client ID or expired short-lived token will cause errors here.
4. Retrieve Long-Lived Facebook Page Access Tokens
Add another HTTP Request node named “Get Long Lived FB Page Token”, connected to the previous node. This node retrieves the pages the user manages and their long-lived page tokens.
Configuration:
- Method: GET
- URL Template:
https://graph.facebook.com/v20.0/{{$node["Set Parameter"].json["app_scoped_user_id"]}}/accounts - Query Parameter:
access_token = {{$json.body.access_token}}(from the previous node’s output)
This dynamically fetches page tokens authorized by the long-lived user token.
Outcome: The output includes page tokens you can use for page management APIs.
Note: If the user ID is missing or incorrect, this request will fail or return empty data.
5. Use Sticky Note for Reference
Add a Sticky Note node with instructions or parameter notes for yourself or team members. This is purely for documentation within the workflow canvas.
Customizations ✏️
- Dynamic Inputs: Replace static values in the Set Parameter node with credentials loaded from n8n’s Credentials Manager for enhanced security.
- Trigger Automation: Replace the manual trigger with a scheduled Cron Trigger to automate token refreshes regularly.
- Error Handling: Add a
Functionnode after HTTP requests to parse responses and catch token expiration errors gracefully. - Output Storage: Add a Google Sheets or Database node to save and log the newly generated tokens for auditing and reuse.
Troubleshooting 🔧
Problem:
“401 Unauthorized” or “Invalid OAuth Access Token” errors on HTTP requests.
Cause: Invalid or expired Facebook App credentials, or short-lived token has expired.
Solution: Double-check and update your client_id, client_secret, and user_access_token in the Set Parameter node. Ensure the short-lived token is active and has the correct permissions.
Problem:
“Missing or invalid user ID” when fetching page tokens.
Cause: The app_scoped_user_id value is incorrect, missing, or not associated with the access token.
Solution: Verify the app-scoped user ID is correct, and that the token belongs to the same user. You can obtain this ID from Facebook Graph API Explorer.
Pre-Production Checklist ✅
- Verify correct Facebook App credentials are entered.
- Ensure the short-lived user access token is valid and not expired.
- Test the workflow manually to confirm tokens are returned successfully.
- Confirm the app-scoped user ID matches the token owner’s ID.
- Backup current tokens and sensitive credentials before production deployment.
Deployment Guide
Activate the workflow by enabling it in your n8n editor.
If you prefer automated refreshes, swap the manual trigger node with a Cron Trigger and set your preferred schedule (e.g., every 1 month).
Monitor workflow executions in the n8n UI to ensure smooth operation and troubleshoot errors.
Conclusion
By building and deploying this n8n workflow, you’ve automated the once tedious process of generating long-lived Facebook user and page access tokens securely and reliably. This saves developers like Sarah multiple hours monthly, reduces human error, and ensures uninterrupted Facebook API integrations.
Next steps could include integrating token storage with secure vaults, expanding automation to handle token renewal alerts, or connecting the tokens to downstream marketing automation pipelines.
You’re now set to maintain your Facebook app tokens effortlessly with n8n!