获取Instagram API的访问令牌

时间:2016-12-13 09:32:44

标签: javascript instagram access-token instagram-api

我想获取访问令牌以便透露帐户的图像。所以我会显示一个用户可以连接的弹出窗口。弹出窗口工作但它重定向到instagram站点,用户连接而不是发送代码。连接的链接类似于:

https://www.instagram.com/accounts/login/?force_classic_login=&next=/oauth/authorize/%3Fclient_id=aaaaaaaa&redirect_uri=url&response_type=token

我登录然后,它将我重定向到:

https://www.instagram.com/oauth/authorize/?client_id=aaaaaaa&redirect_uri=url&response_type=token

我不明白我是如何获得代码的。我还使用了完全相同的代码:https://github.com/radykal/instagram-popup-login

有人能帮助我吗?

修改

var loc = window.location.host+window.location.pathname;

var accessToken = null; //the access token is required to make any endpoint calls, http://instagram.com/developer/endpoints/
    var authenticateInstagram = function(instagramClientId, instagramRedirectUri, callback) {
        //the pop-up window size, change if you want
        var popupWidth = 700,
            popupHeight = 500,
            popupLeft = (window.screen.width - popupWidth) / 2,
            popupTop = (window.screen.height - popupHeight) / 2;
        //the url needs to point to instagram_auth.php
        var popup = window.open('instagram_auth.php', '', 'width='+popupWidth+',height='+popupHeight+',left='+popupLeft+',top='+popupTop+'');
        popup.onload = function() {
            //open authorize url in pop-up
            if(window.location.hash.length == 0) {
                popup.open('https://instagram.com/oauth/authorize/?client_id='+instagramClientId+'&redirect_uri='+instagramRedirectUri+'&response_type=token', '_self');
            }

            //an interval runs to get the access token from the pop-up
            var interval = setInterval(function() {
                try {
                    console.log(window.location);
                    //check if hash exists
                    if(popup.location.hash.length) {
                        //hash found, that includes the access token
                        clearInterval(interval);
                        accessToken = popup.location.hash.slice(14); //slice #access_token= from string
                        popup.close();
                        if(callback != undefined && typeof callback == 'function') callback();
                    }
                }
                catch(evt) {
                    //permission denied
                    console.log("error");
                }
            }, 100);
        }
    };
    function login_callback() {
        alert("You are successfully logged in! Access Token: "+accessToken);
    }
    function login() {
        authenticateInstagram(
            '16edb5c3bc05437594d69178f2aa646a', //instagram client ID
            'localhost/facebook', //instagram redirect URI
            login_callback //optional - a callback function
        );
        return false;
    }

1 个答案:

答案 0 :(得分:1)

代码没问题,我认为你的应用设置存在问题:登录Instagram Developer,转到"管理客户端"和#34;安全"选中一个禁用"隐含OAuth"。