Search code examples
angularjsinstagram-api

Accessing liked images of instagram API with Angular


I'm trying to access the JSON of the liked media of a particular instagram user, in the documentation it says to use this:

https://api.instagram.com/v1/users/self/media/liked?access_token=ACCESS-TOKEN

as mentioned here: https://instagram.com/developer/endpoints/users/

replacing ACCESS-TOKEN with the one given by instagram which I've done below:

(function(){
      var app = angular.module('instafeed', []);
      app.factory("InstagramAPI", ['$http', function($http) {
        return {
          fetchPhotos: function(callback){
            var endpoint = "https://api.instagram.com/v1/users/self/media/liked/?";
            endpoint += "?access_token=[ACCESS-TOKEN]";
            endpoint += "&callback=JSON_CALLBACK";

            $http.jsonp(endpoint).success(function(response){
              callback(response);

            });
          }
        }
      }]);

      app.controller('ShowImages', function($scope, InstagramAPI){
        $scope.layout = 'grid';
        $scope.data = {};
        $scope.pics = [];

        InstagramAPI.fetchPhotos(function(data){
          $scope.pics = data;
          console.log(data)
        });
      });

    })();

obviously I have replaced ACCESS-TOKEN with mine, but nothing is given back, is there something incorrect?

EDIT: I added the callback but still it comes back as undefined.


Solution

  • To make this work using jsonp, add the following to your endpoint url:

    &callback=JSON_CALLBACK
    

    Your callback needs to be named 'JSON_CALLBACK'. Find out why here: https://docs.angularjs.org/api/ng/service/$http#jsonp

    Otherwise, to make a simple GET request...

    $http.get(endpoint).success(function(data){
         callback(data);
    });