(NB If you don’t understand clojure I can traslate the code in Java, just ask please)
I am using scribe with clojure and I am finding some issue to log in with twitter, the code I am using:
(def twitter-service
(-> (doto
(ServiceBuilder.)
(.provider (TwitterApi$Authenticate.))
(.apiKey "apikey")
(.apiSecret "apisecret")
(.callback "https://morgan-siscia.rhcloud.com/tatata/"))
(.build)))
(def tokens (atom {}))
(defn get-token []
(.getRequestToken twitter-service))
(defn get-twitter-url [token]
(swap! tokens assoc (.getToken token) token)
(.getAuthorizationUrl twitter-service token))
(defn get-info-twitter [oauth-token oauth-veri]
(let [token (get @tokens oauth-token)
verifier (Verifier. oauth-veri)
access-token (.getAccessToken twitter-service token verifier)
request (doto (OAuthRequest. (Verb/POST)
"https://api.twitter.com/oauth/access_token")
(.addOAuthParameter "oauth_token" oauth-token)
(.addBodyParameter "oauth_verifier" oauth-veri))]
(do
(.signRequest twitter-service access-token request)
(swap! tokens dissoc oauth-token))
(.getBody (.send request))))
The code is pretty a straight translation of the java code, however it is not working.
When I call (get-info-twitter “the-oauth-code” “the verifier code”) twitter return an error:
<?xml version="1.0" encoding="UTF-8"?>
<hash>
<request>/oauth/access_token</request>
<error>The access_token method must be called with a request_token</error>
</hash>
Honestly I have no idea what I am doing wrong, can somebody help me out ?
Here is how Twitter OAuth works (I do it with Twitter4j, principle should be the same for you) :
User is authorizing your application and a callback URL is invoked by Twitter, passing you 2 parameters : oauth_token and oauth_verifier
With these 2 parameters, you can ask an AccessToken :
requestToken is rebuilt from previous token and secretToken saved somewhere
I save this object in a database and reuse it for later calls to Twitter.
I had the same error as you when I did not understand well all these tokens everywhere.
Try to follow this step-by-step and it should work.