Scribe OAuthRequest to update status Twitter (Null object error)


#1

Hello,

Im trying to update twitter status using java with Scribe library (1.3).

Asking for a proctected resource as in the example works fine. (https://github.com/fernandezpablo85/scribe-java/blob/master/src/test/java/org/scribe/examples/TwitterExample.java)

However, I get the next error when im trying to write a tweet:

Exception in thread “main” java.lang.IllegalArgumentException: Cannot get String from a null object
at org.scribe.utils.Preconditions.check(Preconditions.java:80)
at org.scribe.utils.Preconditions.checkNotNull(Preconditions.java:27)
at org.scribe.utils.StreamUtils.getStreamContents(StreamUtils.java:20)
at org.scribe.model.Response.parseBodyContents(Response.java:41)
at org.scribe.model.Response.getBody(Response.java:67)
at model.TwitterExample.main(TwitterExample.java:84)

Highlight code parts:

OAuthService service = new ServiceBuilder()
.provider(TwitterApi.Authenticate.class)
.apiKey("------------------------------")
.apiSecret("--------------------------------")
.build();
Scanner in = new Scanner(System.in);

// Obtain the Request Token
Token requestToken = service.getRequestToken();

Verifier verifier = new Verifier(in.nextLine());

// Trade the Request Token and Verfier for the Access Token
Token accessToken = service.getAccessToken(requestToken, verifier);</java>

String tweet = URLEncoder.encode(“First Tweet”,“UTF-8”);
String urlTweet=“http://api.twitter.com/1.1/statuses/update.json?status=”+tweet;

System.out.println("request: "+urlTweet);

OAuthRequest request2 = new OAuthRequest(Verb.POST, urlTweet);
service.signRequest(accessToken, request2);
System.out.println("REQUEST: " + request2.getUrl());
Response response2 = request2.send();
System.out.println(response2.getBody());</java>

This exception throws in “response2.getBody()” print.

I have not been able to find the right solution to my problem in your discussions and external forums, so any help is appreciated.

Thank you in advance


#2

Ok, I got it.

I noticed that the way requests works on twitter have changed just a month ago, and now it has to be used https instead of http.

I did some adjustments in my code before this question in reference to this, like using

.provider(TwitterApi.Authenticate.class)
instead of

.provider(TwitterApi.class).

Even so, I did not change this sentence of my code, which was the reason of my error.

String urlTweet=“http://api.twitter.com/1.1/statuses/update.json?status=”+tweet;

to

String urlTweet=“https://api.twitter.com/1.1/statuses/update.json?status=”+tweet;

I hope that if someone comes here with same problem can solve it with this relevant info.

https://github.com/leleuj/pac4j/issues/24

Thanks eltabo for taking me to the right direction.