简体   繁体   English

在Twitter4j中验证用户?

[英]Validate user in Twitter4j?

Is there a way i can make a user enter his username and password and validate his account? 有什么方法可以使用户输入用户名和密码并验证其帐户? My reasoning behind this is making sure the user actually owns that username, and in order to do that, they must have their password. 我这样做的原因是要确保用户实际上拥有该用户名,并且为此,他们必须具有密码。 Is there any way i could verify their credentials and get a returned Boolean whether authentication failed or not? 是否可以通过任何方式验证其凭据并获取返回的布尔值(无论身份验证是否失败)?

Something like this: 像这样:

if(validated)
{
    // Your validated
}
else
{
    // Authentication failed.
}

I am not sure how to do this with the new 1.1 API. 我不确定如何使用新的1.1 API做到这一点。

I guess someone else might know this better, but as a way forward I will just share what I think and it might help you forward on your problem. 我想也许其他人对此会更好,但是作为前进的方向,我将只分享我的想法,这可能会帮助您解决问题。

I don't think it's that straight forward as a method returning a boolean , since you by default do not have access to the users account. 我不认为返回boolean的方法就这么简单,因为默认情况下您无权访问用户帐户。 This is described on twitter4j homepage and I would assume you can follow the example described under Sign in with Twitter , if you are developing a web application. 这在twitter4j主页上进行了描述,并且如果您正在开发Web应用程序,我认为您可以按照“ 使用Twitter登录”下描述的示例进行操作。

If it's a native java application I guess you should go for the OAuth solution where you register your application at the twitter API (To get consumer key/secret) and grant access to validate users. 如果它是本机Java应用程序,我想您应该使用OAuth解决方案,在该应用程序中,您可以在twitter API上注册应用程序(以获取使用者密钥/秘密),并授予访问权限以验证用户。

Basically I think you will end up having the user interact with the twitter login page like seen in the example from twitter4j: 基本上,我认为您最终将使用户与twitter登录页面进行交互,如twitter4j的示例所示:

// The factory instance is re-useable and thread safe.
Twitter twitter = TwitterFactory.getSingleton();
twitter.setOAuthConsumer("[consumer key]", "[consumer secret]");
RequestToken requestToken = twitter.getOAuthRequestToken();
AccessToken accessToken = null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (null == accessToken) {
  System.out.println("Open the following URL and grant access to your account:");
  System.out.println(requestToken.getAuthorizationURL());
  System.out.print("Enter the PIN(if aviailable) or just hit enter.[PIN]:");
  String pin = br.readLine();
  try{
     if(pin.length() > 0){
       accessToken = twitter.getOAuthAccessToken(requestToken, pin);
     }else{
       accessToken = twitter.getOAuthAccessToken();
     }
  } catch (TwitterException te) {
    if(401 == te.getStatusCode()){
      System.out.println("Unable to get the access token.");
    }else{
      te.printStackTrace();
    }
  }
}

YOu have some additional information in this Q: Twitter4J Only Authenticates With Me 您在此问题中还有其他信息: Twitter4J仅通过我进行身份验证

Note that you need Twitter4J version >=3.0 in order to get the API 1.1 support. 请注意,您需要Twitter4J >> 3.0的Twitter4J版本才能获得API 1.1支持。

Hope it helps your forward. 希望它能帮助您前进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM