我开发了一个应用程序和登录od喜欢使用Twitter登录。 我可以获得访问令牌,但后来我无法获得任何用户ID /屏幕名称。 我得到401:身份验证凭据。如果我把这个Twitter对象放到会话中,它就不起作用了。无法找到任何jsf示例。
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Welcome</title>
</h:head>
<h:body>
<f:metadata>
<f:viewParam name="oauth_token" value="#{test.oauth_token}" />
<f:viewParam name="oauth_verifier" value="#{test.oauth_verifier}" />
<f:event type="preRenderView" listener="#{test.yaz()}" />
</f:metadata>
<h:outputText value="#{test.oauth_token}"></h:outputText>
<h:form>
<p:commandButton id="addUser" value="Add"
action="#{test.getToken()}" ajax="false" />
</h:form>
</h:body>
</html>
<pre>
<code>
package temp;
import java.io.IOException;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;
import twitter4j.conf.ConfigurationBuilder;
@ManagedBean(name="test")
@RequestScoped
public class TestOath {
String oauth_token;
String oauth_verifier;
String request_token;
String request_token_secret;
Twitter twitter;
public String getRequest_token() {
return request_token;
}
public String getRequest_token_secret() {
return request_token_secret;
}
public void setRequest_token(String request_token) {
this.request_token = request_token;
}
public void setRequest_token_secret(String request_token_secret) {
this.request_token_secret = request_token_secret;
}
public String getOauth_verifier() {
return oauth_verifier;
}
public void setOauth_verifier(String oauth_verifier) {
this.oauth_verifier = oauth_verifier;
System.out.println("setOauth_token="+oauth_verifier);
}
public String getOauth_token() {
return oauth_token;
}
public void setOauth_token(String oauth_token) {
System.out.println("setOauth_token="+oauth_token);
this.oauth_token = oauth_token;
}
public TestOath(String oauth_token) throws IOException {
this.oauth_token = oauth_token;
}
@Override
public String toString() {
return "TestOath [oauth_token=" + oauth_token + "]";
}
public TestOath() {
super();
}
public void getToken() throws IOException{
String returnText="";
try {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("my apps consumer key")
.setOAuthConsumerSecret("my apps consumer secret")
.setOAuthAccessToken(null)
.setOAuthAccessTokenSecret(null);
TwitterFactory tf = new TwitterFactory(cb.build());
this.twitter = tf.getInstance();
RequestToken requestToken = this.twitter.getOAuthRequestToken();
System.out.println("Got request token.");
System.out.println("Request token: " + requestToken.getToken());
System.out.println("Request token secret: " + requestToken.getTokenSecret());
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
externalContext.redirect(requestToken.getAuthorizationURL());
System.out.println("Test OAUTH is DONE");
FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("yourKey", this.twitter);
}
catch(TwitterException t){
}
}
public void yaz() throws TwitterException
{
System.out.println("111");
if(this.oauth_token == null)
{
System.out.println("not have any access code");
}
else
{
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("i put my apps consumer key")
.setOAuthConsumerSecret("i put my apps consumer secret")
.setOAuthAccessToken(this.oauth_token)
.setOAuthAccessTokenSecret(this.oauth_verifier);
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter t = tf.getInstance();
System.out.println(t.getId());
}
return ;
}
public void parameterOnLoad(FacesContext fc) {
Map<String,String> params = fc.getExternalContext().getRequestParameterMap();
String oauth_token = params.get("oauth_token");
System.err.println("oauth_token="+oauth_token);
}
}
</code><pre>