문제

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.

도움이 되었습니까?

해결책

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. 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.

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.

Basically I think you will end up having the user interact with the twitter login page like seen in the example from 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

Note that you need Twitter4J version >=3.0 in order to get the API 1.1 support.

Hope it helps your forward.

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