简体   繁体   English

Instagram API:语法错误:意外的令牌

[英]Instagram API: SyntaxError: Unexpected token <

I'm using the 'tag/media/recent?'我正在使用“标签/媒体/最近?” endpoint of the Instagram API and I'm getting an error. Instagram API 的端点,我收到一个错误。 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.当发送的值不可用时,我知道我收到了一个 404 页面,因为标签不可用,但我该如何处理错误。 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.使用 Twitter API,我可以搜索屏幕名称“hemidemisemiquaver”,它会返回我的个人资料数据作为后备,因为没有该名称的个人资料。 With the Instagram API, it returns a 404 page.使用 Instagram API,它返回一个 404 页面。

The Instagram API documentation states I should get an response object that looks like this: Instagram API 文档指出我应该得到一个如下所示的响应对象:

{
"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)在 if 条件使用 (error || response.statusCode != 200) insted of (error && response.statusCode != 200)

Update: remove the space after the tags/ in the url.更新:删除 url 中 tags/ 之后的空格。

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 :)我希望这会奏效:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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