0

I have been trying to use different Java OAuth libraries to get request and access token from Twitter API. Finally, I have come to use Scribe as one of the most stable libraries for this matter. However, I have not been able to get this to work with Twitter on the first step which is getting the access token:

twitterOAuthService = new ServiceBuilder(twitterOauthConsumerKey)
          .apiSecret(twitterOauthConsumerKey).debug().callback(callback).build(
              TwitterApi.instance());
      OAuth1RequestToken requestToken = twitterOAuthService.getRequestToken();

It throws the following exception:

Response{code=401, message='Authorization Required', body='null', headers={date=Tue, 05 Nov 2019 07:06:34 GMT, null=HTTP/1.1 401 Authorization Required, server=tsa_l, content-length=64, expires=Tue, 31 Mar 1981 05:00:00 GMT, x-response-time=166, x-frame-options=SAMEORIGIN, www-authenticate=OAuth realm="https://api.twitter.com", x-transaction=00e2c19300b2d1ab, strict-transport-security=max-age=631138519, pragma=no-cache, set-cookie=guest_id=v1%3A157293759473549885; Max-Age=63072000; Expires=Thu, 4 Nov 2021 07:06:34 GMT; Path=/; Domain=.twitter.com, last-modified=Tue, 05 Nov 2019 07:06:34 GMT, x-xss-protection=0, x-content-type-options=nosniff, content-disposition=attachment; filename=json.json, x-connection-hash=badf004485a6d713a29ed3f90de9e981, x-twitter-response-tags=BouncerCompliant, content-type=application/json; charset=utf-8, cache-control=no-cache, no-store, must-revalidate, pre-check=0, post-check=0, status=401 Unauthorized}}

I can see that the Authorization header is set properly:

Authorization -> OAuth oauth_callback="http%253A%252F%252F127.0.0.1%253A3000%252Fsettings", oauth_consumer_key="8hupaLbXA2s792EzU8DIE6K", oauth_nonce="2929183554", oauth_signature="s%2BRMAninTESJqJg92BuEA8h3E%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1572936780", oauth_version="1.0"

I have tested the below approaches as they were suggested in different places:

  • Ensure Authorization header is sorted alphabetically!

  • Ensure that the timestamp is set properly

  • Ensure that the consumer secret and consumer key are set properly

  • Ensure that the encoding of oauth_signature (percent encoding) and call_back is set properly

I have run out of any other scenarios and I am not even sure if I can use Scribe library for Twitter. I would appreciate it if someone could help me to address this issue.

Ali
  • 1,759
  • 2
  • 32
  • 69
  • Does this help? https://stackoverflow.com/a/59765764/1776132 – Smile Jan 17 '20 at 02:38
  • @Smile Thanks, but my issue was resolved as I realized I was encoding the callback URL twice as it was done already in the library. – Ali Jan 17 '20 at 04:04

0 Answers0