Вопрос

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

  • you ask a RequestToken :

RequestToken requestToken = twitter.getOAuthRequestToken();

  • From this, you get 2 objects : a token and a tokenSecret :

String token = requestToken.getToken(); String secretToken = requestToken.getTokenSecret();

  • 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 :

AccessToken accessToken = twitter.getOAuthAccessToken(requestToken, oauth_verifier);

requestToken is rebuilt from previous token and secretToken saved somewhere

  • This new object contains 2 String you have to keep for future calls to Twitter :

credentials.token = accessToken.getToken();

credentials.secret = accessToken.getTokenSecret();

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.

Другие советы

Why don't you use clojure-twitter? It would be cleaner.

Example:

(:require twitter [oauth.client :as oauth])

 ;; Make a OAuth consumer
 (def oauth-consumer (oauth/make-consumer 
                      twitter-key
                      twitter-secret
                      "https://api.twitter.com/oauth/request_token"
                      "https://api.twitter.com/oauth/access_token"
                      "https://api.twitter.com/oauth/authorize"
                      :hmac-sha1))

And then:

(twitter/with-oauth oauth-consumer
     oauth-access-token
     oauth-access-token-secret
     (print (twitter/user-timeline :screen-name username)))

https://github.com/mattrepl/clojure-twitter

The oauth_verifier parameter comes in the callback. Use that one

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top