문제

I am trying to implement Twitter login process. During the login process a user need to be redirected to a Twitter web-site to enter his/her credentials and then he/she will be redirected to my website URL. Before the first redirect an instance of RequestToken object (Twitter4J library) should be stored and retained between requests.

To do it I decided to use ConverstaionScoped bean, but unfortunately values of references are not retained between requests.

Here are my JSF pages:

twitter.xhtml:

<html xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      >

    <f:event listener="#{twitterLoginPageController.login()}" type="preRenderView" />

</html>

twitterRedirect.xhtml:

<html xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      >

    <f:event listener="#{twitterLoginPageController.onRedirect(param.oauth_token, param.oauth_verifier)}" type="preRenderView" />

</html>

My Twitter login controller:

@Named
@ConversationScoped
public class TwitterLoginPageController implements Serializable {


    @Inject
    private Conversation conversation;

    // These objects should be retained between requests
    Twitter twitter;
    RequestToken requestToken;

    public void login() {

        try {
            twitter = new TwitterFactory().getInstance();
            requestToken = twitter.getOAuthRequestToken("...");


            conversation.begin();
            conversation.setTimeout(30000);
            // Navigate to Twitter here
        } catch (TwitterException ex) {
             ...
        }

    }

    public void onRedirect(String oauthToken, String oauthVerifier) {      
        try {
            // twitter reference is null here
            twitter.getOAuthAccessToken(requestToken, oauthVerifier);
            ...
        } catch (TwitterException e) {
            ...
        } finally {
            conversation.end();
        }

    }
}

It seems to me that I am following examples of using @ConverstaionScope closely, but I fail to get the expected result. What should I do to retain objects between requests?

도움이 되었습니까?

해결책

I'm not familiar with Twitter auth or Twitter4J specifically, but since it is twitter that redirects the user back to your application, you must pass twitter the conversation id such that twitter redirects the user back to an URL that includes that id.

The Java EE container maintains the conversation state by passing the cid=... parameter around and that happens automatically for JSF navigation, but you have to take care of it otherwise. So make sure that you obtain the conversation id after you start the conversation and pass it to Twitter such that twitter redirects the user to twitterRedirect.xhtml?cid=....

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top