Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2e17a1b

Browse files
author
Ryan Neufeld
committed
Merge pull request clojure-cookbook#133 from codebrickie/twitter-pr
Work on Twitter recipe
2 parents f0f9602 + 810b6d0 commit 2e17a1b

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

network-io/twitter-status-update/twitter-status-update.asciidoc renamed to network-io/twitter-api/twitter-api.asciidoc

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
==== Send a Twitter Status Update
1+
==== Using the Twitter API
22
// By Tobias Bayer (codebrickie)
33

44
===== Problem
55

6-
You want to send a status update to Twitter on behalf of a Twitter user.
6+
You want to connect to Twitter and use its API.
77

88
===== Solution
99

@@ -18,8 +18,7 @@ Include the wrapper in your project file:
1818
:dependencies [[twitter-api "0.7.4"]])
1919
----
2020

21-
This recipe assumes you have already retrieved an OAuth consumer key and secret for your app from Twitter and an access token and secret for the user.
22-
SEE RECIPE XYZ ON HOW TO RETRIEVE AN ACCESS TOKEN VIA OAUTH.
21+
This recipe assumes you have already retrieved an OAuth footnote:[See the Twitter documentation on how to receive OAuth credentials at https://dev.twitter.com/docs/auth/obtaining-access-tokens] consumer key and secret for your app from Twitter and an access token and secret for the user. Make sure your app has read/write access in order to post status updates.
2322

2423
Define your keys and secrets and send the status update request to Twitter:
2524

@@ -36,12 +35,12 @@ Define your keys and secrets and send the status update request to Twitter:
3635
(statuses-update :oauth-creds credentials :params {:status "Hi there, I am sending this tweet from Clojure!"})
3736
;; -> {:status {:code 200, :msg "OK",...
3837
----
39-
38+
4039

4140
===== Discussion
4241

43-
Twitter-api offers more than just sending status updates.
44-
The structure of function calls basically remains the same. Twitter-api functions take the credentials and request parameters as arguments.
42+
++twitter-api++ offers more than just sending status updates.
43+
The structure of function calls basically remains the same. ++twitter-api++ functions take the credentials and request parameters as arguments.
4544

4645
For example, you can show user details with ++users-show++:
4746

@@ -51,5 +50,25 @@ For example, you can show user details with ++users-show++:
5150
;; -> ...:id 40514394, :profile_background_image_url_https "https://si0.twimg.com/images/themes/theme1/bg.png", :description "Without being overly modest, you can read this because of me.", :profile_text_color "333333", :screen_name "Alan_M_Turing"...
5251
----
5352

53+
Using Twitter's streaming feature for reading a user's timeline is also possible.
54+
First you should define a callback function that will be called later every time a new tweet is received:
55+
56+
[source,clojure]
57+
----
58+
(use '[twitter.api.streaming] '[twitter.callbacks] '[twitter.callbacks.handlers])
59+
(import '(twitter.callbacks.protocols AsyncStreamingCallback))
60+
61+
(def ^:dynamic *print-status-callback*
62+
(AsyncStreamingCallback. #(println %2) nil nil))
63+
----
64+
65+
This function simply prints the raw output stream to the console.
66+
Now request the user stream and provide your callback:
67+
68+
[source,clojure]
69+
----
70+
(user-stream :oauth-creds credentials :callbacks *print-status-callback*)
71+
----
72+
5473
===== See Also
5574
See the https://github.com/adamwynne/twitter-api[twitter-api documentation] for more details.

0 commit comments

Comments
 (0)