Search code examples
javascriptjqueryajaxinstagram-api

Error occurs while calling Instagram user API. Just died in GET Method


I am working on Instagram User API. 'self' API works fine, but '{user_id}' does not work.

Here is their API DOC. https://www.instagram.com/developer/endpoints/users/

This is my ajax code to get user information.

function setInstagramLogin(btnId, userId){
    var target = document.getElementById(btnId);
    var user = (userId == null || typeof userId == 'undefined') ? 'self' : userId;
    target.addEventListener('click', function(){
        var loginUrl = 'https://api.instagram.com/v1/users/' + user + '/?callback=?';
        console.log(loginUrl);
        $.getJSON(loginUrl, access_parameters, function(instagramData){
            console.log(instagramData);
            var userInfo = instagramData.data;
            alert('Your Information\n username [ ' + userInfo.username + ' ]\n'
                    + 'profile Photo [ ' + userInfo.profile_picture + ' ]\n');
        });

    }, false);
}

And from jsp, I call like ...

setInstagramLogin('loginBtn');
setInstagramLogin('loginBtn', 'jygood');

The first one, call 'self' works, the other does not. How can I fix this?

The error happens inside of callback jQuery I guess. Here is chrome inspector error

GET https://api.instagram.com/v1/users/jygood/?callback=jQuery21109406823019962…s_token=211611405.1677ed0.4e0e854196454874b858e21c293b919d&_=1450936187539

send @ jquery-2.1.1.min.js:4

n.extend.ajax @ jquery-2.1.1.min.js:4

n.(anonymous function) @ jquery-2.1.1.min.js:4

n.extend.getJSON @ jquery-2.1.1.min.js:4

(anonymous function) @ instagram.js:29

line29 is $.getJSON() part of first script.

Thanks!


Solution

  • the + user + should be an id not instagram username

    https://api.instagram.com/v1/users/jygood/?callback=
    

    looks like you are using jygood username instead of user_id, it should be an integer value