mardi 29 août 2017

Origin 'http://localhost:3000' is therefore not allowed access Twitter API

I'm developing a web application that uses the Twitter API REST. I have a page with Twitter accounts and a button for adding a new account. When the button is pressed, the function in the Angular controller is executed:

// Calls token to add a new Twitter user
    $scope.callToken = function () {
        $http.get('/token')
            .then(function (response) {
                console.log(response);
            });
    };

This is the code in the backend to serve de request. It simply redirects to Twitter to add a new user.

// routes.js
var token = express.Router();

        token.route('/token')
                .get(cuentasCtrl.getToken);
    
// Account file

exports.getToken = function(req, res) {

        twitter.getRequestToken(function(err, requestToken, requestSecret) {
        if (err)
            res.status(500).send(err);
        else {
            _requestSecret = requestSecret;
            res.setHeader('Access-Control-Allow-Origin', '*');
            res.redirect("https://api.twitter.com/oauth/authenticate?oauth_token=" + requestToken);
        }
    });
};

But I get the next error:

Origin 'http://localhost:3000' is therefore not allowed access Twitter API

My server is running on localhost:3000 and If I put localhost:3000/token in my browser there is no problem. I have read the solutions for using CORS and tested other browsers but it hasn't worked for me. What have i done wrong?




Aucun commentaire:

Enregistrer un commentaire