0

I'm making a next.js website and using the library Twitter-lite, to access the Twitter API. For some reason the code run normaly in my local server, but when I send it to vercel, to publish it, it doesn't work, It returs

502: BAD_GATEWAY
Code: NO_RESPONSE_FROM_FUNCTION
ID: gru1::dzfhn-1612303490166-f7e971564512

the code is:

import Twitter from "twitter-lite";

export default async function getUserTweets(request, response) {
    var amount = 10
    
    const client = new Twitter({
        subdomain: "api",
        consumer_key: process.env.TWITTER_API_KEY,
        consumer_secret: process.env.TWITTER_SECRET_API_KEY,
        access_token_key: process.env.TWITTER_TOKEN,
        access_token_secret: process.env.TWITTER_TOKEN_SECRET,
        bearer_token:process.env.TWITTER_BEARER_TOKEN
    });
    
    
    let timeline = await client.get("statuses/user_timeline", {
        screen_name: "nerat0",
        exclude_replies: true,
        include_rts: false,
        tweet_mode: "extended",
        count: amount + 2
    });
    response.json(timeline)
}

OBS: in vercel Function Log it says: errors: [ { code: 32, message: 'Could not authenticate you.' } ]. But how could I run normaly in local with the same authentication...

Can anyone help me with a link or a explanation? Thanks :)

Renato
  • 121
  • 1
  • 8
  • Have you checked you have the right environment variables when deployed to Vercel? – juliomalves Feb 03 '21 at 10:26
  • In my local server the keys works, can I check it otherwise? Another thing, I already use another API that uses key, and this one works fine – Renato Feb 03 '21 at 14:00
  • Add a debugging block like `process.env.TWITTER_API_KEY ? 'Env var defined' : throw \`Unexpected value ${process.env.TWITTER_API_KEY }\`` – paulogdm Feb 03 '21 at 14:43

1 Answers1

0

Basically the problem is in .env file and vars. If you are at your onw server (local) the next js will get the enviroment variables right, but when you send it to vercel the vercel server try to get those .env vars from another file, not from this one (.var), you can read more about it here:

https://nextjs.org/docs/basic-features/environment-variables

Environment variables not working (Next.JS 9.4.4)

Renato
  • 121
  • 1
  • 8