OAuth Request Token returning empty object

Hi,

I am using twitter for login in my web application.

While using localhost, the twitter oAuth api to request OAuth token works fine but when i deploy it to a server, it gives an empty object instead of oAuth token.

1 Like

Thanks for reaching out! Do you have a code sample you can send over on this?

1 Like

Thanks much for replying @jessicagarson ,

Here is the backend code from which i hit the request oAuth tokn api, followed by the frontend code to call the same.

oauthRouter.route('/:employeeId/:userAccessToken')
    .all((req, res, next) => {
        res.statusCode = 200;
        res.setHeader('Content-Type', 'application/json');
        next();
    })
    .get((req, res, next) => {
        res.statusCode = 200;
        res.setHeader('Content-Type', 'application/json');
        var oauth = {
            callback: 'https://socialstars.policybazaar.com/social-login'
            , consumer_key: '<App_Consumer_key>'
            , consumer_secret: '<App_consumer_secret>'
        }
        var url = 'https://api.twitter.com/oauth/request_token';
        users.findOne({ 'employeeId': req.params.employeeId, "authDetails.accessToken": { "$in": req.params.userAccessToken } }).then((doc) => {
            if (doc != null) {
                request.post({ url: url, oauth: oauth }, function (e, r, body) {
                    var req_data = qs.parse(body)
                    res.json(req_data);
                })

            }
            else {
                res.json({
                    "error": true,
                    "message": "invalid session"
                })
            }
        })

    })

frontend code

 const path = `/firstStepTwitter/${employeeId}/${token}`;

    const httpOptions = {
      headers: new HttpHeaders({
        'Content-Type': 'application/json'
      })
    };

    this.http.get(url + path, httpOptions).subscribe((res: any) => {
      if (res) {
        console.log('request token response =====> ', res);
        const oAuthToken = res.oauth_token;
        localStorage.setItem('oauthsecret', res.oauth_token_secret);
        localStorage.setItem('oauthtoken', res.oauth_token);
        this.isLoading = false;
        this.authService.loadingListener.next(this.isLoading);
        window.open(`https://api.twitter.com/oauth/authenticate?oauth_token=${oAuthToken}`, '_self');
        console.log('first step', res);
      } else {
        this.isLoading = false;
        this.authService.loadingListener.next(this.isLoading);
        this.toast.showError('Login Failed!');
      }
    });

Now this works fine on localhost but when i deploy it, the response of request token api is an empty object.

1 Like

@jessicagarson facing the same issue please look into it

Hello @mappie! Can you share some code?

I’m posting code of two methods which worked on localhost but did not work on server.

var oauth = { callback: ‘xxxxxxxxxxxxxxxxxxxxx’
, consumer_key:‘XXXXX’
, consumer_secret:‘XXXXXX’
} request.post({url:url, oauth:oauth}, function (e, r, body) {
var req_data = qs.parse(body)
res.json(req_data);
})

I tried using twitter node client too even that did not work

var twitter = new Twitter({
“consumerKey”: “xxxxxxxxxxxxxxxx”,
“consumerSecret”: “xxxxxxxxxxxxxxx”,
“accessToken”: “xxxxxxxxxxxxxxxxx”,
“accessTokenSecret”: “xxxxxxxxxxxxxxx”,
“callBackUrl”: “xxxxxxxxxxxxxxxxxxxxx”});
twitter.getOAuthRequestToken((error)=>{
res.json({“r”:error});
},(oauth)=>{
res.json({“r”:qs.parse(oauth)});
})

@jessicagarson @andypiper please take a look into my code

Thanks for following up here and the code sample. I have a few other follow up questions:

  • How are you currently hosting this code?
  • Have you tried it on another machine or network?
  • Are there any known issues with your network or any kind of proxying you’re passing through?
1 Like

Hi @jessicagarson thanks much for following up,

This is the error response we are getting while hitting the twitter request token endpoint :

{“error”:{“errno”:-104,“code”:“ECONNRESET”,“syscall”:“read”}}

Have a look at sockets - How do I debug error ECONNRESET in Node.js? - Stack Overflow for clues - this sounds like a networking / connection error on your server.

Try running twurl or something on the server to test if you can successfully connect to twitter API servers from your server, and there’s nothing blocking requests like a firewall.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.