Instagram api返回帖子链接

时间:2017-02-13 14:30:23

标签: javascript instagram instagram-api

我正在使用Instagram图片制作图库,现在我想要链接我的图库,我想当用户点击图片时,它想要在Instagram帖子上重定向他。所以现在我需要从instagram api获得链接。 现在我只有图片,我就这样得到了:

jQuery.ajax({
    url: 'https://api.instagram.com/v1/users/' + userid + '/media/recent',
    dataType: 'jsonp',
    type: 'GET',
    data: {access_token: token, count: num_photos},
    success: function(data){ ........

有人可以帮助我如何获取帖子网址吗?

1 个答案:

答案 0 :(得分:0)

Instagram API开始,您应该使用链接参数来获取每张照片的链接。此代码应位于成功(数据)功能中。使用 / users / self / media / recent 端点,它可能会被调用:

[...]
success: function(data){
    for (i = 0; i < data.length; i++){ 
        var photoURL = data[i].images.standard_resolution.url;
        var photoLink = data[i].link;
        var username = data[i].user.username;

        var html = "<a href='" + photoLink + "' target='_blank'>";
        html += "    <img src='" + photoURL + "' alt='" + username + "'>";
        html += "</a>";

        // here you should append this html code to some container box
        document.getElementById("myContainer").innerHTML += html;
    }
}

上面的代码未经过测试。但我相信它运作良好。 以下是media object returned by Instagram,因此您可以选择所需的任何信息。我隐藏了users_in_photo和图片部分以便于理解。

{
"data": {
    "type": "image",
    "users_in_photo": [{...}],
    "filter": "Walden",
    "tags": [],
    "comments": { "count": 2 },
    "caption": null,
    "likes": { "count": 1 },
    "link": "http://instagr.am/p/D/",
    "user": {
        "username": "kevin",
        "full_name": "Kevin S",
        "profile_picture": "...",
        "id": "3"
    },
    "created_time": "1279340983",
    "images": {...},
    "id": "3",
    "location": null
}
}

照片部分中的用户:

"users_in_photo": [{
        "user": {
            "username": "kevin",
            "full_name": "Kevin S",
            "id": "3",
            "profile_picture": "..."
        },
        "position": {
            "x": 0.315,
            "y": 0.9111
        }
    }],

图片部分:

"images": {
        "low_resolution": {
            "url": "http://distillery.s3.amazonaws.com/media/2010/07/16/4de37e03aa4b4372843a7eb33fa41cad_6.jpg",
            "width": 306,
            "height": 306
        },
        "thumbnail": {
            "url": "http://distillery.s3.amazonaws.com/media/2010/07/16/4de37e03aa4b4372843a7eb33fa41cad_5.jpg",
            "width": 150,
            "height": 150
        },
        "standard_resolution": {
            "url": "http://distillery.s3.amazonaws.com/media/2010/07/16/4de37e03aa4b4372843a7eb33fa41cad_7.jpg",
            "width": 612,
            "height": 612
        }
    },
祝你好运!