简体   繁体   English

Instagram API提供的access_token无效

[英]Instagram API The access_token provided is invalid

I am a new programmer and quite new to .js in general. 我是一个新程序员,对.js来说还是一个新手。 My current issue is that when I run this script Instagram.popular(function(r){ console.log(r)}) locally (ie on the console of Firefox) I get the following error The access_token provided is invalid 我当前的问题是,当我在本地(即在Firefox的控制台上)运行此脚本Instagram.popular(function(r){ console.log(r)}) ,出现以下错误The access_token provided is invalid

As I read in gitHub and other places, it has something do with Instagram changing some API policy as discussed here . 当我在gitHub和其他地方阅读时,这与Instagram更改此处讨论的某些API策略有关。 The change log for Instagram is given here . 这里提供Instagram的更改日志。

If anyone can point me in the right direction, I will very much appreciate it. 如果有人能指出正确的方向,我将非常感谢。

Below you can see my full .js code. 在下面,您可以看到我的完整.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 + '" />' );
                }
            });

        });

    });

The first thing you should start from is by hitting Instagram popular link in the browser. 首先,您应该点击浏览器中的Instagram流行链接。 you will notice that you are getting invalid access token in response as well. 您还会注意到,您也将获得无效的访问令牌。 This is happening because instagram API requires authentication which is done through OAuth . 发生这种情况是因为instagram API需要通过OAuth进行身份验证。 You have to find a js implementation of OAuth or a 3rd party js liberary with OAuth implementation and use it to obtain access token and use to get resources in subsequent server calls. 您必须找到OAuth的js实现或具有OAuth实现的第三方js库,并使用它来获取访问令牌并用于在后续的服务器调用中获取资源。 you should also reference Instagram Developers website as well to see how they suggest implementing it. 您还应该参考Instagram开发者网站 ,以了解他们建议如何实施它。

You are using some old code that uses client_id to make API calls. 您正在使用一些使用client_id进行API调用的旧代码。

Instagram stopped allowing API calls with just client_id since June 1, 2016. Now you have to have an access_token in URL param to make API calls, you have to do the oauth authentication and get access_token as described in documentation: 自2016年6月1日起,Instagram停止仅允许使用client_id进行API调用。现在,您必须在URL参数中具有access_token才能进行API调用,您必须执行oauth身份验证并按照文档中的说明获取access_token

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

And also from your code /media/popular/ is not valid anymore, its a deprecated, looks like you are using some old Instagram library 而且从您的代码/media/popular/中不再有效,它已被弃用,看起来您正在使用一些旧的Instagram库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM