External API - 401 error - Twitter API - Works on Postman

Options

Hi everybody,

I'm trying to do a basic call to Twitter

  • API GET /2/users/by/username/:username
  • I've copied the cURL into my function
  • Added my Twitter Bearer token as an Environment variable
  • The Beare token is pushed into the Headers array.

I always get a 401 back. That same request in Postman works fine, using exactly the same Bearer token.

I've been blindly trying things for a while, can't see what I'm missing. Help please 🙏🏼

That's the look of my function:

— The response —

{

request: {

url: https://api.twitter.com/2/users/by/username/TwitterDev,

method: GET,

headers: [

GET /2/users/by/username/TwitterDev HTTP/2,

Host: api.twitter.com,

accept-encoding: gzip, deflate,

content-type: application/json,

accept: application/json

],

params: []

},

response: {

headers: [

HTTP/2 401,

perf: 7626143928,

content-type: application/problem+json,

cache-control: no-cache, no-store, max-age=0,

content-length: 99,

x-transaction-id: f6cf6db836763e1f,

x-response-time: 1,

x-connection-hash: 1b230d0571be9d81f61b891f01d5cf8e45fc4a3575c3421f54695d23b2b54217,

date: Sat, 25 Feb 2023 09:22:19 GMT,

server: tsa_b

],

result: {

title: Unauthorized,

type: about:blank,

status: 401,

detail: Unauthorized

},

status: 401

}

}

Best Answer

  • Carlos
    Carlos Member
    Answer ✓
    Options

    Thanks Ray for passing by,

    I did try adding “Authorization: Bearer” to my token variable and still didn't work (I know is not showing in the request I've pasted here). Also hardcoded it directly in the request and nothing.

    Not sure what it was tbh, in part also because my QA process was a mess… not my forte specially when tired, it might be that I had it misspelled or something similar

Answers

  • Carlos
    Carlos Member
    Options

    Solved.

    I've generated the cURL from Postman itself and that did the trick, is working now.

    However, I'm comparing both requests and honestly don't see the difference.

    Anyway, if your external API works on Postman but not in Xano go to code, right side menu, below Send, copy the cURL and import it into Xano

    Keep pushing!

  • Ray Deck
    Ray Deck Trusted Xano Expert ✭✭✭
    Options

    That's great! It seems clear now that your problem was that the original had just the token by itself as a header, without the “Authorization: Bearer” text that needed to precede it.

    Clever to build it in Postman and then curl-copy it to Xano - will put that technique in my toolbox for the future!