简体   繁体   中英

Instagram API: SyntaxError: Unexpected token <

I'm using the 'tag/media/recent?' endpoint of the Instagram API and I'm getting an error. When the value sent over isn't available, I understand that I'm getting a 404 page because the tag isn't available but how do I handle the error. With the Twitter API, I could search for the screen name 'hemidemisemiquaver' and it'll return my profile data as a fallback because there is no profile by that name. With the Instagram API, it returns a 404 page.

The Instagram API documentation states I should get an response object that looks like this:

{
"meta": {
    "error_type": "OAuthException",
    "code": 400,
    "error_message": "..."
 }
}

I don't understand why this is happening. Does you understand why?

    app.post('/instaInputQuery', function (req, res, next) {
    console.log('INPUT_QUERY: ' + typeof req.body.query); // returns string
    var popular_tag_search_tag_name_recent = {
        url: 'https://api.instagram.com/v1/tags/ ' + req.body.query + '/media/recent?access_token=' + tokenContainer[0] + '&count=200',
        method: 'GET'
    };

request(popular_tag_search_tag_name_recent, function (error, response, body) {
    if (error && response.statusCode != 200) {
        console.error(error);
        res.send(error);
    } else {
        var JSONobjArray = JSON.parse(body);
        console.log('*******************************************************'.black.bgGreen);
        console.log(JSONobjArray);
        console.log('*******************************************************'.black.bgGreen);
        res.send(JSONobjArray);
    }
});
});

Thank you Vishnu for sending solving half the problem!

In if condition use (error || response.statusCode != 200) insted of (error && response.statusCode != 200)

Update: remove the space after the tags/ in the url.

try this:

app.post('/instaInputQuery', function (req, res, next) {

var popular_tag_search_tag_name_recent = {
    url: 'https://api.instagram.com/v1/tags/' + req.body.query + '/media/recent?access_token=' + tokenContainer[0] + '&count=200',
    method: 'GET'
};

request(popular_tag_search_tag_name_recent, function (error, response, body) {
    if (error || response.statusCode != 200) {
        error = error || response;
        console.error(error);
        res.send(error);
    } else {
        var JSONobjArray = JSON.parse(body);
        console.log('*******************************************************'.black.bgGreen);
        console.log(JSONobjArray);
        console.log('*******************************************************'.black.bgGreen);
        res.send(JSONobjArray);
    }
});
});

I hope this will work :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM