Guest User

Untitled

a guest
Jan 2nd, 2016
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.55 KB | None | 0 0
  1. Step 1: Verifying Details
  2. require 'rubygems'
  3. require 'oauth'
  4.  
  5. # Change the following values to those provided on dev.twitter.com
  6. # The consumer key identifies the application making the request.
  7. # The access token identifies the user making the request.
  8. consumer_key = OAuth::Consumer.new(
  9. "YOUR_CONSUMER_KEY",
  10. "YOUR_CONSUMER_SECRET")
  11. access_token = OAuth::Token.new(
  12. "YOUR_ACCESS_TOKEN",
  13. "YOUR_ACCESS_SECRET")
  14.  
  15. # All requests will be sent to this server.
  16. baseurl = "https://api.twitter.com"
  17.  
  18. # The verify credentials endpoint returns a 200 status if
  19. # the request is signed correctly.
  20. address = URI("#{baseurl}/1.1/account/verify_credentials.json")
  21.  
  22. # Set up Net::HTTP to use SSL, which is required by Twitter.
  23. http = Net::HTTP.new address.host, address.port
  24. http.use_ssl = true
  25. http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  26.  
  27. # Build the request and authorize it with OAuth.
  28. request = Net::HTTP::Get.new address.request_uri
  29. request.oauth! http, consumer_key, access_token
  30.  
  31. # Issue the request and return the response.
  32. http.start
  33. response = http.request request
  34. puts "The response status was #{response.code}"
  35.  
  36. Step 2 : Parsing a User Object
  37.  
  38. require 'rubygems'
  39. require 'oauth'
  40.  
  41. # Parse a response from the API and return a user object.
  42. def parse_user_response(response)
  43. user = nil
  44.  
  45. # Check for a successful request
  46. if response.code == '200'
  47. # Parse the response body, which is in JSON format.
  48. # ADD CODE TO PARSE THE RESPONSE BODY HERE
  49. user =
  50.  
  51. # Pretty-print the user object to see what data is available.
  52. puts "Hello, #{user["screen_name"]}!"
  53. else
  54. # There was an error issuing the request.
  55. puts "Expected a response of 200 but got #{response.code} instead"
  56. end
  57.  
  58. user
  59. end
  60.  
  61. # All requests will be sent to this server.
  62. baseurl = "https://api.twitter.com"
  63.  
  64. # Verify credentials returns the current user in the body of the response.
  65. address = URI("#{baseurl}/1.1/account/verify_credentials.json")
  66.  
  67. # Set up HTTP.
  68. http = Net::HTTP.new address.host, address.port
  69. http.use_ssl = true
  70. http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  71.  
  72. # If you entered your credentials in the previous
  73. # exercise, no need to enter them again here. The
  74. # ||= operator will only assign these values if
  75. # they are not already set.
  76. consumer_key ||= OAuth::Consumer.new "ENTER IN EXERCISE 1", ""
  77. access_token ||= OAuth::Token.new "ENTER IN EXERCISE 1", ""
  78.  
  79. # Issue the request.
  80. request = Net::HTTP::Get.new address.request_uri
  81. request.oauth! http, consumer_key, access_token
  82. http.start
  83. response = http.request(request)
  84. user = parse_user_response(response)
  85.  
  86. Step 3: Reading a tweet
  87.  
  88. require 'rubygems'
  89. require 'oauth'
  90. require 'json'
  91.  
  92. # Now you will fetch /1.1/statuses/show.json, which
  93. # takes an 'id' parameter and returns the
  94. # representation of a single Tweet.
  95. baseurl = "https://api.twitter.com"
  96. path = "/1.1/statuses/show.json"
  97. query = URI.encode_www_form("id" => "TWEET ID GOES HERE")
  98. address = URI("#{baseurl}#{path}?#{query}")
  99. request = Net::HTTP::Get.new address.request_uri
  100.  
  101. # Print data about a Tweet
  102. def print_tweet(tweet)
  103. # ADD CODE TO PRINT THE TWEET IN "<screen name> - <text>" FORMAT
  104. end
  105.  
  106. # Set up HTTP.
  107. http = Net::HTTP.new address.host, address.port
  108. http.use_ssl = true
  109. http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  110.  
  111. # If you entered your credentials in the first
  112. # exercise, no need to enter them again here. The
  113. # ||= operator will only assign these values if
  114. # they are not already set.
  115. consumer_key ||= OAuth::Consumer.new "ENTER IN EXERCISE 1", ""
  116. access_token ||= OAuth::Token.new "ENTER IN EXERCISE 1", ""
  117.  
  118. # Issue the request.
  119. request.oauth! http, consumer_key, access_token
  120. http.start
  121. response = http.request request
  122.  
  123. # Parse and print the Tweet if the response code was 200
  124. tweet = nil
  125. if response.code == '200' then
  126. tweet = JSON.parse(response.body)
  127. print_tweet(tweet)
  128. end
  129.  
  130. Step 4: Reading a timeline
  131.  
  132. require 'rubygems'
  133. require 'oauth'
  134. require 'json'
  135.  
  136. # Now you will fetch /1.1/statuses/user_timeline.json,
  137. # returns a list of public Tweets from the specified
  138. # account.
  139. baseurl = "https://api.twitter.com"
  140. path = "/1.1/statuses/user_timeline.json"
  141. query = URI.encode_www_form(
  142. "screen_name" => "twitterapi",
  143. "count" => 5,
  144. )
  145. address = URI("#{baseurl}#{path}?#{query}")
  146. request = Net::HTTP::Get.new address.request_uri
  147.  
  148. # Print data about a list of Tweets
  149. def print_timeline(tweets)
  150. # ADD CODE TO ITERATE THROUGH EACH TWEET AND PRINT ITS TEXT
  151.  
  152. end
  153.  
  154. # Set up HTTP.
  155. http = Net::HTTP.new address.host, address.port
  156. http.use_ssl = true
  157. http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  158.  
  159. # If you entered your credentials in the first
  160. # exercise, no need to enter them again here. The
  161. # ||= operator will only assign these values if
  162. # they are not already set.
  163. consumer_key ||= OAuth::Consumer.new "ENTER IN EXERCISE 1", ""
  164. access_token ||= OAuth::Token.new "ENTER IN EXERCISE 1", ""
  165.  
  166. # Issue the request.
  167. request.oauth! http, consumer_key, access_token
  168. http.start
  169. response = http.request request
  170.  
  171. # Parse and print the Tweet if the response code was 200
  172. tweets = nil
  173. if response.code == '200' then
  174. tweets = JSON.parse(response.body)
  175. print_timeline(tweets)
  176. end
  177. nil
  178.  
  179. Step 5 : Sending a tweet
  180.  
  181. require 'rubygems'
  182. require 'oauth'
  183. require 'json'
  184.  
  185. # You will need to set your application type to
  186. # read/write on dev.twitter.com and regenerate your access
  187. # token. Enter the new values here:
  188. consumer_key = OAuth::Consumer.new(
  189. "READ/WRITE CONSUMER KEY",
  190. "READ/WRITE CONSUMER SECRET")
  191. access_token = OAuth::Token.new(
  192. "READ/WRITE ACCESS TOKEN",
  193. "READ/WRITE ACCESS SECRET")
  194.  
  195. # Note that the type of request has changed to POST.
  196. # The request parameters have also moved to the body
  197. # of the request instead of being put in the URL.
  198. baseurl = "https://api.twitter.com"
  199. path = "/1.1/statuses/update.json"
  200. address = URI("#{baseurl}#{path}")
  201. request = Net::HTTP::Post.new address.request_uri
  202. request.set_form_data(
  203. "status" => "",
  204. )
  205.  
  206. # Set up HTTP.
  207. http = Net::HTTP.new address.host, address.port
  208. http.use_ssl = true
  209. http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  210.  
  211. # Issue the request.
  212. request.oauth! http, consumer_key, access_token
  213. http.start
  214. response = http.request request
  215.  
  216. # Parse and print the Tweet if the response code was 200
  217. tweet = nil
  218. if response.code == '200' then
  219. tweet = JSON.parse(response.body)
  220. puts "Successfully sent #{tweet["text"]}"
  221. else
  222. puts "Could not send the Tweet! " +
  223. "Code:#{response.code} Body:#{response.body}"
  224. end
Advertisement
Add Comment
Please, Sign In to add comment