Question

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());

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!

Was it helpful?

Solution

Try checking response2.getCode() before response2.getBody(), maybe you're getting an error (such a 403 - forbidden) and you're ignoring it.

Hope it helps.

OTHER TIPS

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 https://github.com/juliendangers/pac4j/commit/72d025e219dd11f854ecf1f8156e48f688d55615

Thanks eltabo for taking me to the right direction.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top