Revoltechs
Revoltechs

Reputation: 205

Getting a list of my followers and followed using Instagram API

I'm trying to get a list of all my followers and everyone I follow on Instagram using the Instagram API. I think that my response is returning null whenever I try to get my followers.

This is the error I get:

Cannot read property 'length' of undefined

This is in reference to response.data.length

Here's my app.js:

window.Instagram = {

    /**
     * Store application settings
     */
    config: {},

    BASE_URL: 'https://api.instagram.com/v1',

    init: function(opt) {
        opt = opt || {};

        this.config.client_id = opt.client_id;
    },

    /**
     * Get a list of popular media
     */
    popular: function(callback) {
        var endpoint = this.BASE_URL + '/media/popular?client_id=' + this.config.client_id;
        this.getJSON(endpoint, callback);
    },

    /**
     * Get a list of recently tagged media
     */
    tagsByName: function(name, callback) {
        var endpoint = this.BASE_URL + '/tags/' + name + '/media/recent?client_id=' + this.config.client_id;
        this.getJSON(endpoint, callback);
    },

    followers: function(callback) {
        var endpoint = this.BASE_URL + '/users/self/follows?access_token=' + this.config.access_token;
        this.getJSON(endpoint, callback);
    },

    getJSON: function(url, callback) {
        $.ajax({
            type: 'GET',
            url: url,
            dataType: 'jsonp',
            success: function(response) {
                if (typeof callback === 'function') callback(response);
            }
        });
    }
}

Instagram.init({

        client_id: 'CENSORED',
    access_token: 'CENSORED'

});

$(document).ready(function() {

    Instagram.followers(function(response) {
        var $instagram = $('#instagram');
        $instagram.append('<textarea cols="1" rows="' + response.data.length + '">')
        for (var i = 0; i < response.data.length; i++) {
            var name = response.data[i].username;
            $instagram.append(name + '&#13;&#10;');
        }
        $instagram.append('</textarea>');
    });

});

Upvotes: 2

Views: 3834

Answers (1)

krisrak
krisrak

Reputation: 12952

Are you in sandbox mode? if so it is expected to get empty response, read documentation about Sandbox mode: https://www.instagram.com/developer/sandbox/

(Also I noticed you have code for '/media/popular?client_id=', the popular API was deprecated as of June 1st 2016, so you probably are using some old Instagram library code. - You need to read updated documentation and re-implement everything)

Upvotes: 1

Related Questions