How do I renew a Facebook user_access_token if I deal with a lot of AJAX?

后端 未结 7 1084

Please tell me if I\'m understanding correctly. (because I might not be.)

  1. User posts something on my site. (He checked \"also post to Facebook\".)
  2. Client
7条回答
  •  爱一瞬间的悲伤
    2021-02-09 13:29

    I've got the problem in another project.

    The way I handle it is to create a hidden iframe. The first time you need the user to accept the privilege, use your main window to redirect. then, when you are sure that the user has already accepted the privilege, use the hidden iframe to communicate with facebook.

    The user will not see the "flash" because it will done in an iframe.

    I've done it with GWT. Here is the code I used : it communicates with Facebook via the iframe and do a check on access token every 500ms to see if the token is valid.

    The code is in java (compiled in javascript using gwt).

    public class FacebookConnector extends Composite
    {
            public static final String                                      ARG_ACCESS_TOKEN_EXPIRES        = "fb_accessTokenExpires";
            public static final String                                      ARG_GAME_FACEBOOK_NAME          = "gameFBName";
            public static final String                                      ARG_GAME_FACEBOOK_ID            = "gameFBId";
    
            private static FacebookConnectorUiBinder        uiBinder                                        = GWT.create(FacebookConnectorUiBinder.class);
    
            interface FacebookConnectorUiBinder extends UiBinder
            {
            }
    
            private static FacebookConnector        me;
    
            public static FacebookConnector getInstance()
            {
                    if (me == null)
                    {
                            me = new FacebookConnector();
                    }
    
                    return me;
            }
    
            @UiField
            IFrameElement   iframe;
    
            private Date    accessToken;
            private Timer   timer;
    
            protected FacebookConnector()
            {
                    initWidget(uiBinder.createAndBindUi(this));
    
                    if (ArgManager.getArg(ARG_ACCESS_TOKEN_EXPIRES) != null)
                    {
                            accessToken = new Date(Long.parseLong(ArgManager.getArg(ARG_ACCESS_TOKEN_EXPIRES)));
                    }
            }
    
            public void checkAccessToken(final AbstractAsyncCallback callback)
            {
                    if (accessToken == null || accessToken.before(new Date()))
                    {
                            // send authentication
                            String url = "https://graph.facebook.com/oauth/authorize?client_id="
                                            + ArgManager.getArg(ARG_GAME_FACEBOOK_ID) + "&scope=user_birthday,email&redirect_uri="
                                            + ArgManager.getArg(ArgManager.ARG_URL_FACEBOOK_BASE) + "page/facebook-step2%3FgameName%3D"
                                            + ArgManager.getGameShortcut();
    
                            iframe.setSrc(url);
    
                            // check url
                            timer = new Timer() {
    
                                    @Override
                                    public void run()
                                    {
                                            ClientFactory.getInstance().getService().getAccessTokenExpires(new AbstractAsyncCallback() {
    
                                                    @Override
                                                    public void onSuccess(Date result)
                                                    {
                                                            super.onSuccess(result);
    
                                                            if (result != null && result.after(new Date()))
                                                            {
                                                                    accessToken = result;
    
                                                                    // call the callback
                                                                    callback.onSuccess(null);
                                                            }
                                                            else
                                                            {
                                                                    // try again in one second
                                                                    timer.schedule(1000);
                                                            }
    
                                                    }
    
                                            });
    
                                    }
                            };
    
                            // launch timer in 500 milliseconds
                            timer.schedule(500);
                    }
                    else
                    {
                            callback.onSuccess(null);
                    }
            }
    }
    

    Hope it will help you.

提交回复
热议问题