Bearer token not working from expression

Describe the issue/error/question

I’m having a weird behavior when setting a bearer token with expression. Basically the expression is “Bearer {{$node[“Get Agency Keys”].json[“highLevelKey”]}}”.

The resultant value looks correct but I get the error {“msg”:“Unauthorized”} when the request is made.

Here is the part that seems odd. If I copy the token from the expression result window below the expression, then paste it in the expression as a hard-coded value, then the request works.

So I have confirmed the token value itself is not the issue.

The source node for the token is called only once so it’s not changing on each call.
I don’t see any extra leading or trailing spaces.
The token itself is static, not needing to be refreshed.

Is there something obvious I could be missing?

What is the error message (if any)?

{“msg”:“Unauthorized”}

Please share the workflow

Share the output returned by the last node

[
  {
    "id": "Tab2YgfUYkZTRRmdYgd1Bb”,
    "locationId": "PabQkbmGYgaYXiOxFJXSNc”,
    "contactName": “joe smith”,
    "firstName": “joe”,
    "lastName": “smith,
    "companyName": “Acme Co.”,
    "email": “[email protected],
    "phone": "+15555555555”,
    "dnd": false,
    "type": "customer",
    "source": "sub account user",
    "assignedTo": null,
    "city": null,
    "state": null,
    "postalCode": null,
    "address1": null,
    "dateAdded": "2022-11-04T16:47:59.125Z",
    "dateUpdated": "2022-11-18T15:32:49.767Z",
    "dateOfBirth": null,
    "tags": [
      "sub-account-user"
    ],
    "lastActivity": 1667580481764,
    "customField": [
      {
        "id": "YYyy5LNzidDh9UefuBmX",
        "value": "xfpHOGL1uuku3GKVyd7b"
      },
      {
        "id": "userId",
        "value": "ny0raTuYfOqzELSCz8rL"
      },
      {
        "id": "IIRzhyjPfEHP6rtb0kV8",
        "value": "admin"
      },
      {
        "id": "KgbBylJ15PEUccMpwMZ3",
        "value": "account"
      }
    ],
    "userId": "ny0raTuYfOqzELSCz8rL"
  },
  {
    "id": "rabDXZiGQEgKM6wuf26ZdO",
    "locationId": "PabQkbmGYgaYXiOxFJXSNc",
    "contactName": “harry potter”,
    "firstName": “harry”,
    "lastName": "potter,
    "companyName": “Fake Company inc.”,
    "email": “[email protected]”,
    "phone": null,
    "dnd": false,
    "type": "customer",
    "source": "sub account user",
    "assignedTo": null,
    "city": null,
    "state": null,
    "postalCode": null,
    "address1": null,
    "dateAdded": "2022-11-03T16:51:06.498Z",
    "dateUpdated": "2022-11-18T15:32:50.208Z",
    "dateOfBirth": null,
    "tags": [
      "sub-account-user",
      "add-user-membership"
    ],
    "customField": [
      {
        "id": "YYyy5LNzidDh9UefuBmX",
        "value": "be1gN9XKwIQrdlStYdfW"
      },
      {
        "id": "usertId",
        "value": "u2fq4NE6HXoL2XkfUhN2"
      },
      {
        "id": "IIRzhyjPfEHP6rtb0kV8",
        "value": "user"
      },
      {
        "id": "KgbBylJ15PEUccMpwMZ3",
        "value": "account"
      }
    ],
    "userId": "u2fq4NE6HXoL2XkfUhN2"
  }
]

Information on your n8n setup

  • n8n version: 0.202.1
  • Database you’re using (default: SQLite): SQLLite
  • Running n8n with the execution process [own(default), main]: ?
  • Running n8n via [Docker, npm, n8n.cloud, desktop app]: Elestio VM

Hey @mbowler,

Do you have more than one item coming from the Get Keys or Agency nodes?

Only a single item is being returned from each of the “keys” nodes.

Have you tried changing the URL to post to something like webhook.site so you can double check what is being sent?

There is no body to send. It’s just a GET request with an ID in the endpoint URL. I was looking for a way to see the request that gets sent by N8N but I can’t find a place to view that.

To inspect the full HTTP request you could send it to a URL provided by a service such as webhook.site @mbowler.

That said, your example data looks like you have two items:

Seeing you use an expression of Bearer {{$node["Get Agency Keys"].json["highLevelKey"]}} in your HTTP Request node, but you have only a single item on your highLevelKey node, what seems to happen here would be this: HTTP node misses the JSON payload for 2nd iteration - #2 by MutedJam

So, in addition to sending your data to webhook.site for debugging, you might also want to try out an expression such as Bearer {{ $("Get Agency Keys").first().json.highLevelKey }} to make sure you always use the first (and only) item from your Get Agency Keys node.