The documentation on this is fairly bad. Followed guides, still encountering problems.
Relates to OAuth on the Twitter API via the Twitter4J library.
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthConsumerKey("");
builder.setOAuthConsumerSecret("");
builder.setOAuthAccessToken(null);
builder.setOAuthAccessTokenSecret(null);
Configuration configuration = builder.build();
TwitterFactory factory = new TwitterFactory(configuration);
Twitter twitter = factory.getInstance();
RequestToken requestToken;
try {
String callbackURL = "/callback";
requestToken = twitter.getOAuthRequestToken(callbackURL);
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:");
String requestTokenAuthorisationURL = requestToken.getAuthorizationURL();
System.out.println(requestTokenAuthorisationURL);
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();
}
}
}
} catch (TwitterException ex) {
Logger.getLogger(EmployerAdvertisingTwitter.class.getName()).log(Level.SEVERE, null, ex);
}
The null pointer exception is being thrown on this line;
if (pin.length() > 0) {
And I cannot see why this is happening or what this section of code actually does. I've tried commenting out that whole try/catch section of code surrounding the pin.length()
piece of code and this ends up in an endless loop generating new authorisation URLs.
Any ideas or pointers?