1

I'm trying to create a custom module for a deployd backend to authenticate with the instagram api. The code is as follows:

var request = require('request');

var payload = undefined;

process.server.on('request', function(req, res) {
if (req.url === '/foo') {

    function getPayload(callback) {
        var code = req.headers.referer.substr(req.headers.referer.lastIndexOf('/') + 1);

        request.post('https://api.instagram.com/oauth/access_token', {form:{
            client_id: '****',
            client_secret: '****',
            grant_type: 'authorization_code',
            redirect_uri: 'http://localhost:2403/auth/auth.html',
            code: code
        }}, 
        function(error, response, body) {
            var payload = JSON.stringify(body);
            console.log(payload);
            callback();
        });
    }

    function setPayload() {
        res.end(payload);
    }

    getPayload(setPayload);
}
});

I can console.log the response from the instagram server, but then when I try to write the response back to the client I get a 404 response. What am I missing? Sorry if this is a total noob question. That would be because I'm a noob. Thanks in advance.

Sorry for the lack of clarity. Edit for more info: I'm creating an endpoint at /foo with the above code. The client sends a GET request to /foo from a referring URL containing the Instagram access code. The server then POSTs the access code along with the other app-specific parameters to the Instagram server which returns an object containing an authorization token and information about the user. The 404 comes at the point where the client GETs /foo. My code successfully sends the access code to the Instagram server and returns the object containing the access token - this outputs on the console - but it won't write it back to the client, and the client gets a 404. Also, the 404 is returned to the client before the object returned by the Instagram server is logged in the console.

bangley
  • 11
  • 2
  • What is the redirect URI that you use in the authorization request? – fajarkoe Jun 29 '14 at 06:21
  • you mean the client is getting a 404 making a request to /auth/auth.html ? – Catalyst Jun 29 '14 at 07:03
  • @Catalyst I added more information. Thanks for your help! – bangley Jun 29 '14 at 11:34
  • @fajarkoe the redirect URI is /auth/auth.html, but I don't think it's relevant here as the user is not redirected - Instagram returns an object including the access token at the second stage of the authorization flow. Thanks for your input! – bangley Jun 29 '14 at 11:42
  • @bangley I just want to make sure that in access token request you use the same redirect URI as the one you send in authorization request. Is the authorization code that you send in access token request the same as the one returned in the the authorization response? – fajarkoe Jun 29 '14 at 12:59
  • @fajarkoe Thanks. Yes I'm sending the same redirect URI! – bangley Jun 29 '14 at 13:36
  • I think that the redirect is potentially ignoring the port number, and assuming port 80. I've had problems with that before – Catalyst Jun 29 '14 at 17:14

0 Answers0