AutomationFlowsSocial Media › Facebook Token Retrieval & Management

Facebook Token Retrieval & Management

ByL Hùng @meow-cde on n8n.io

A Facebook Developer account with an active app. Basic understanding of n8n workflows. Access to a database (optional, for storing tokens). Webhook Activation: Configure the Webhook to receive user requests and process input data. Ensure the Webhook URL is correctly set in your…

Webhook trigger★★★★☆ complexity12 nodesHTTP Request
Social Media Trigger: Webhook Nodes: 12 Complexity: ★★★★☆ Added:

This workflow corresponds to n8n.io template #2959 — we link there as the canonical source.

The workflow JSON

Copy or download the full n8n JSON below. Paste it into a new n8n workflow, add your credentials, activate. Full import guide →

Download .json
{
  "id": "MVhWWLZv7PhhLSq2",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Facebook Token Mew",
  "tags": [],
  "nodes": [
    {
      "id": "ecd428ff-673c-435c-a6b3-723ae6f3f88d",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "position": [
        1680,
        280
      ],
      "parameters": {
        "path": "facebook-login",
        "options": {},
        "responseMode": "responseNode"
      },
      "typeVersion": 2
    },
    {
      "id": "7fe9fcb7-49f3-4628-8a9f-2027f9dffd61",
      "name": "If",
      "type": "n8n-nodes-base.if",
      "position": [
        2360,
        280
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "loose"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "ce391d94-03af-42d1-ae73-91bdb2e93c00",
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              },
              "leftValue": "={{ $('Webhook').first().json.query.code }}",
              "rightValue": "login"
            }
          ]
        },
        "looseTypeValidation": true
      },
      "typeVersion": 2.2
    },
    {
      "id": "ded6d8fa-3add-4084-a9f2-172a5d51158b",
      "name": "Short-Lived Token",
      "type": "n8n-nodes-base.httpRequest",
      "onError": "continueErrorOutput",
      "position": [
        2720,
        140
      ],
      "parameters": {
        "url": "https://graph.facebook.com/v21.0/oauth/access_token",
        "options": {},
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "client_id",
              "value": "={{ $('Config').first().json.app_id }}"
            },
            {
              "name": "redirect_uri",
              "value": "={{ $('Config').first().json.fb_redirect_uri }}"
            },
            {
              "name": "client_secret",
              "value": "={{ $('Config').first().json.app_secret }}"
            },
            {
              "name": "code",
              "value": "={{ $('Webhook').first().json.query.code }}"
            }
          ]
        }
      },
      "retryOnFail": true,
      "typeVersion": 4.2,
      "alwaysOutputData": false,
      "waitBetweenTries": 5000
    },
    {
      "id": "27fed69c-e476-4e19-870e-f26ed707e36a",
      "name": "Long-Lived Token",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        3000,
        -100
      ],
      "parameters": {
        "url": "https://graph.facebook.com/v21.0/oauth/access_token",
        "options": {},
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "grant_type",
              "value": "fb_exchange_token"
            },
            {
              "name": "client_id",
              "value": "={{ $('Config').first().json.app_id }}"
            },
            {
              "name": "client_secret",
              "value": "={{ $('Config').first().json.app_secret }}"
            },
            {
              "name": "fb_exchange_token",
              "value": "={{ $('Short-Lived Token').first().json.access_token }}"
            }
          ]
        }
      },
      "retryOnFail": true,
      "typeVersion": 4.2,
      "waitBetweenTries": 5000
    },
    {
      "id": "94e38177-b72f-4e99-9e2f-e43bd679589b",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1660,
        -800
      ],
      "parameters": {
        "width": 920,
        "height": 900,
        "content": "### Instructions to Get a 2-Month Facebook Token\nRun n8n with tunnel model - Or user: Ngrok, Cloudflare Tunnel\n\n1. **Activate the App**:\n   - Launch the app. In the top-right corner near the **Save** button, you will see a status button labeled **Inactive**.\n   - Switch this button to **Active** (it will turn green) to activate the app.\n\n2. **Access the Webhook URL**:\n   - Visit the following URL (fb_redirect_uri):  \n      https://localhost:5678/webhook/facebook-login?type=login\n\n3. **Follow the Steps**:\n   - Complete the on-screen instructions to finish the process.\n   - Once done, you will receive a **Facebook Token** valid for **2 months (60 days)**.\n\n\n### Instructions to Check Token Status and Get Page Token\n1. **Check Token Status**:\n   - Go to [Facebook Access Token Debugger](https://developers.facebook.com/tools/debug/accesstoken/).\n   - Paste your **User Token** into the debugger to view details such as expiration date, permissions, and token status.\n\n2. **Get Page Token for Posting**:\n   - A **User Token** only grants access to personal information. To post on a Page, you need a **Page Token**.\n   - Send a GET request to the following endpoint (replace `{user-access-token}` with your User Token):\n     ```\n     GET https://graph.facebook.com/v21.0/me/accounts?access_token={user-access-token}\n     ```\n   - The response will include a list of Pages you manage along with their corresponding **Page Tokens**.\n\n3. **Use the Page Token**:\n   - Use the **Page Token** to perform actions such as posting, managing comments, or other tasks on the Page.\n\n**Note**:\n- Ensure the User Token has the necessary permissions like `pages_manage_posts`, `pages_read_engagement`, etc., to retrieve the Page Token.\n- The Page Token will have the same expiration as the User Token (approximately 60 days) and can be renewed.\n\n\n`https://github.com/luuhung93/n8n-json`"
      },
      "typeVersion": 1
    },
    {
      "id": "4af7f1f6-9479-4489-9282-7083cd29649c",
      "name": "Redirect URL",
      "type": "n8n-nodes-base.code",
      "position": [
        2980,
        480
      ],
      "parameters": {
        "jsCode": "const config = $('Config').first().json;\n\n\nconst generateRandomState = () => {\n\treturn Math.random().toString(36).substring(2, 15) +\n\t\tMath.random().toString(36).substring(2, 15);\n}\nconst state = generateRandomState();\n\nconst correctScopes = [\n        'publish_video',\n\t\t'pages_show_list',\n\t\t'business_management',\n        'pages_read_engagement',\n        'pages_read_user_content',\n        'pages_manage_metadata',\n        'pages_manage_posts',\n        'pages_manage_engagement',\n\t];\n\nconst authUrl = `https://www.facebook.com/v22.0/dialog/oauth?client_id=${config.app_id}&redirect_uri=${config.fb_redirect_uri}&scope=${correctScopes.join(',')}&response_type=code&state=${state}`;\n\nreturn [{\n  authUrl\n}];"
      },
      "typeVersion": 2
    },
    {
      "id": "b415e387-17ed-4451-ae7d-b8d1fdf8ee06",
      "name": "Redirect",
      "type": "n8n-nodes-base.respondToWebhook",
      "position": [
        3740,
        480
      ],
      "parameters": {
        "options": {},
        "redirectURL": "={{ $json.authUrl }}",
        "respondWith": "redirect"
      },
      "typeVersion": 1.1
    },
    {
      "id": "d59290d9-79b5-4212-a8ad-a1a9e074bbf2",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1840,
        140
      ],
      "parameters": {
        "width": 420,
        "height": 480,
        "content": "Edit:\n - fb_redirect_uri\n - app_id\n - app_secret\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nGo to the [Facebook App](https://developers.facebook.com/apps/).\n    - Select **Facebook Login** from the left-hand menu.\n    - Click on **Settings**.\n    - In the **Valid OAuth Redirect URIs** section, add the URL (fb_redirect_uri): `https://localhost:5678/webhook/facebook-login`"
      },
      "typeVersion": 1
    },
    {
      "id": "952f17fa-efdf-4523-9d9b-ff071670e425",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2460,
        460
      ],
      "parameters": {
        "width": 640,
        "height": 760,
        "content": "\n\n\n\n### Guide to Editing `correctScopes`\n\n1. **Add a Permission**:\n   - Add the permission to the `correctScopes` array. For example:\n     ```javascript\n     const correctScopes = [\n       'pages_manage_posts', // Existing permission\n       'pages_messaging',    // New permission added\n     ];\n     ```\n\n2. **Edit a Permission**:\n   - Replace a permission in the `correctScopes` array. For example:\n     ```javascript\n     const correctScopes = [\n       'pages_manage_posts', // Existing permission\n       'pages_messaging',    // New permission replacing the old one\n     ];\n     ```\n\n3. **Remove a Permission**:\n   - Remove a permission from the `correctScopes` array. For example:\n     ```javascript\n     const correctScopes = [\n       'pages_manage_posts', // Keep this permission\n       // 'pages_messaging',  // Remove this permission\n     ];\n     ```\n\n4. **View Available Permissions**:\n   - Refer to the full list of permissions at [Facebook Permissions](https://developers.facebook.com/docs/permissions/).\n\nSimply edit the `correctScopes` array; no need to worry about other parts.\n"
      },
      "typeVersion": 1
    },
    {
      "id": "dbc712ba-1a95-4041-ac27-824d804726bc",
      "name": "Token",
      "type": "n8n-nodes-base.respondToWebhook",
      "position": [
        3700,
        -100
      ],
      "parameters": {
        "options": {},
        "respondWith": "text",
        "responseBody": "={{ $json.access_token }}"
      },
      "typeVersion": 1.1
    },
    {
      "id": "560d3c3f-a0e8-432e-904f-71cd7525fd0d",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        3280,
        -180
      ],
      "parameters": {
        "width": 280,
        "content": "By default, the token will be displayed on the screen. However, you can add a `node` to save the token into your database."
      },
      "typeVersion": 1
    },
    {
      "id": "99e67d66-5179-4186-b78f-ced80ab5dd82",
      "name": "Config",
      "type": "n8n-nodes-base.set",
      "position": [
        1980,
        280
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "c210cd1e-ff2d-47cb-b9c4-af1e9a91ec71",
              "name": "fb_redirect_uri",
              "type": "string",
              "value": "https://localhost:5678/webhook/facebook-login"
            },
            {
              "id": "3f24bd07-905f-4c3c-bd07-2661e0a95d7a",
              "name": "app_id",
              "type": "string",
              "value": "12345678900000"
            },
            {
              "id": "51de3a11-3e7f-4c8e-9c2c-63ae28a14eb8",
              "name": "app_secret",
              "type": "string",
              "value": "aaabbbcccceedb6f9c91993c871"
            }
          ]
        }
      },
      "typeVersion": 3.4
    }
  ],
  "active": false,
  "settings": {
    "timezone": "Asia/Ho_Chi_Minh",
    "callerPolicy": "workflowsFromSameOwner",
    "executionOrder": "v1"
  },
  "versionId": "c2d0b26b-3366-424a-a741-20649907f008",
  "connections": {
    "If": {
      "main": [
        [
          {
            "node": "Short-Lived Token",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Redirect URL",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Config": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Webhook": {
      "main": [
        [
          {
            "node": "Config",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Redirect URL": {
      "main": [
        [
          {
            "node": "Redirect",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Long-Lived Token": {
      "main": [
        [
          {
            "node": "Token",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Short-Lived Token": {
      "main": [
        [
          {
            "node": "Long-Lived Token",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Redirect URL",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
Pro

For the full experience including quality scoring and batch install features for each workflow upgrade to Pro

About this workflow

A Facebook Developer account with an active app. Basic understanding of n8n workflows. Access to a database (optional, for storing tokens). Webhook Activation: Configure the Webhook to receive user requests and process input data. Ensure the Webhook URL is correctly set in your…

Source: https://n8n.io/workflows/2959/ — original creator credit. Request a take-down →

More Social Media workflows → · Browse all categories →

Related workflows

Workflows that share integrations, category, or trigger type with this one. All free to copy and import.

Social Media

Instagram - Fluxo de mensagens. Uses rabbitmq, rabbitmqTrigger, googleSheets, httpRequest. Webhook trigger; 74 nodes.

Rabbitmq, Rabbitmq Trigger, Google Sheets +1
Social Media

Automated n8n workflow: Receives videos via form, dubs/translates them to the selected languages, and—upon completion—uploads them to multiple social media channels and cloud drives, including Box, Dr

HTTP Request, Telegram, Box +3
Social Media

Automate your post-event Instagram carousel using a fan-out and merge pattern. One Code node splits the photos array into individual n8n items. Every photo then flows through HTTP Fetch, Upload to URL

HTTP Request, N8N Nodes Uploadtourl, Airtable +1
Social Media

My workflow 3. Uses httpRequest, youTube. Webhook trigger; 17 nodes.

HTTP Request, YouTube
Social Media

This workflow automates the process of creating an AI-generated avatar video using HeyGen and directly uploading it to YouTube. By sending text input via a webhook, the workflow generates a video with

HTTP Request, YouTube