scribe + twitter > Cannot get String from a null o

2019-05-22 07:54发布

问题:

I try to use scribe for authorization with facebook and twitter. There is no problems with facebook. But when I try

requestToken = service.getRequestToken()

for twitter I get

Cannot get String from a null object

> ru.myx.ae3.exec.ExecErrorDefault
    > Cannot get String from a null object
    > java.lang.IllegalArgumentException
      : org.scribe.utils.Preconditions.check(Preconditions.java:84)
      : org.scribe.utils.Preconditions.checkNotNull(Preconditions.java:31)
      : org.scribe.utils.StreamUtils.getStreamContents(StreamUtils.java:22)
      : org.scribe.model.Response.parseBodyContents(Response.java:43)
      : org.scribe.model.Response.getBody(Response.java:69)
      : org.scribe.oauth.OAuth10aServiceImpl.getRequestToken(OAuth10aServiceImpl.java:60)
      : org.scribe.oauth.OAuth10aServiceImpl.getRequestToken(OAuth10aServiceImpl.java:40)
      : org.scribe.oauth.OAuth10aServiceImpl.getRequestToken(OAuth10aServiceImpl.java:45)
      : sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      : sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      : sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      : java.lang.reflect.Method.invoke(Method.java:606)

Could anybody help me to fix it?

回答1:

Fortunately, Scribe has SSL support, you just need to use the correct TwitterAPI class inside the builder (the one intended for SSL connections:

final OAuthService service = new ServiceBuilder()
.provider(**TwitterApi.SSL.class**)
.apiKey(...)
.apiSecret(...)
.callback(...)
.build();

Worked for me like a charm :)



回答2:

I had encountered this problem too...

What I did was I created a custom TwitterApi like bellow. For some reason, twitter oauth stopped supporting http and only supports ssl. see below.

import org.scribe.builder.api.DefaultApi10a;
import org.scribe.model.Token;


public class CustomTwitterApi extends DefaultApi10a {

private static final String AUTHORIZATION_URL = "https://api.twitter.com/oauth/authorize?oauth_token=%s";

public String getRequestTokenEndpoint() {
    return "https://api.twitter.com/oauth/request_token";
}

public String getAccessTokenEndpoint() {
    return "https://api.twitter.com/oauth/access_token";
}

public String getAuthorizationUrl(Token requestToken) {
    return String.format(AUTHORIZATION_URL, requestToken.getToken());
}
}


标签: scribe