Search code examples
facebook-oauth

How to obtain a new access token for a Facebook App


By mistake, I removed my App from my account which of course disabled the access token. I have been following the steps from Facebook's Authentication Doc page to get a new access token and finally had success but to no avail; the access token, actually the several tokens I have generated do not work.

Can someone provide some direction?


Solution

  • If you are using the javascript SDK - this is your best path:

        <div id="fb-root">
    </div>
    <script type="text/javascript">
        (function () {
            var e = document.createElement('script'); e.async = true;
            e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
            document.getElementById('fb-root').appendChild(e);
        } ());
    
        window.fbAsyncInit = function () {
            FB.init({ appId: 'YOUR_APP_ID',
                status: true,
                cookie: true,
                xfbml: true,
                oauth: true
            });
    
            FB.getLoginStatus(function (response) {
                if (response.status === 'connected') {
                    var uid = response.authResponse.userID;
                    //you access token is right below
                    var accessToken = response.authResponse.accessToken;
                    console.log('connected and allowed');
                } else if (response.status === 'not_authorized') {
                    console.log('connected but not allowed');
                    top.location.href = "https://www.facebook.com/dialog/oauth?client_id=YOUR_APP_ID&redirect_uri=YOUR_RETURN_URL&scope=user_photos,publish_actions";
                } else {
                    // the user isn't even logged in to Facebook.
                    top.location.href = "https://www.facebook.com/dialog/oauth?client_id=YOUR_APP_ID&redirect_uri=YOUR_RETURN_URL&scope=user_photos,publish_actions";
                }
            });
        };
    
    
    </script>