2016-11-21 86 views
0

我是一名新程序员,一般对.js颇为陌生。 我现在的问题是,当我(火狐的控制台上即)运行此脚本Instagram.popular(function(r){ console.log(r)})本地我收到以下错误The access_token provided is invalidInstagram API提供的access_token无效

正如我在GitHub上和其他地方看,它有什么做的Instagram改变某些API政策如讨论的here。 Instagram的更改日志给出here

如果任何人都可以指出我正确的方向,我将非常感激。

您可以在下面看到我的完整.js代码。

CODE

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); 
     }, 

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

    Instagram.init({ 
     client_id: '97bf259cc45f4dd6a2cd02b694b7ffe7' 
    }); 


    $(document).ready(function() { 

     Instagram.popular(function(response) { 
      var $instagram = $('#instagram'); 
      for (var i = 0; i < response.data.length; i++) { 
       imageUrl = response.data[i].images.low_resolution.url; 
       $instagram.append('<img src="' + imageUrl + '" />'); 
      } 
     }); 

     $('#form').on('submit', function(e) { 
      e.preventDefault(); 

      var tagName = $('#search').val(); 
      Instagram.tagsByName(tagName, function(response) { 
       var $instagram = $('#instagram'); 
        $instagram.html(''); 

       for (var i = 0; i < response.data.length; i++) { 
        imageUrl = response.data[i].images.low_resolution.url; 
        $instagram.append('<img src="' + imageUrl + '" />'); 
       } 
      }); 

     }); 

    }); 

回答

1

你应该是通过点击浏览器Instagram popular链接开始的第一件事。您会注意到您也收到了无效的访问令牌。发生这种情况是因为instagram API需要通过OAuth进行验证。您必须通过OAuth实现找到js实现的OAuth或第三方js liberary,并使用它获取访问令牌并用于在随后的服务器调用中获取资源。您还应该参考Instagram Developers website以了解他们如何建议实施它。

1

您正在使用一些使用client_id进行API调用的旧代码。

Instagram的停止允许API调用,只需client_id自6月1日,2016年现在,你必须有在网址PARAM的access_token调用API,你必须做的OAuth认证,并在文档中描述得access_token

https://www.instagram.com/developer/authentication/

而且还从你的代码/media/popular/不再有效,其弃用的,看起来像你使用的是一些旧的Instagram库