forked from xdevplatform/Twitter-API-v2-sample-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_a_list.rb
89 lines (66 loc) · 2.9 KB
/
update_a_list.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# This script implements the PIN-based OAuth flow to obtain access tokens for a user context request
# It then makes a User lookup request (by usernames) with OAuth 1.0a authentication (user context)
require 'oauth'
require 'json'
require 'typhoeus'
require 'oauth/request_proxy/typhoeus_request'
# The code below sets the consumer key and secret from your environment variables
# To set environment variables on Mac OS X, run the export command below from the terminal:
# export CONSUMER_KEY='YOUR-KEY', CONSUMER_SECRET='YOUR-SECRET'
consumer_key = ENV["CONSUMER_KEY"]
consumer_secret = ENV["CONSUMER_SECRET"]
# Be sure to add replace update-name-of-list with the name you wish to call the list.
# name, description and private are all optional
@json_payload = { "name": "update-name-of-list-ruby",
"description": "update-description-of-list",
"private": false}
# Be sure to replace your-list-id with the id of the list you wish to update. The authenticated user must own the list in order to update
id = "your-list-id"
update_list_url = "https://api.twitter.com/2/lists/#{id}"
consumer = OAuth::Consumer.new(consumer_key, consumer_secret,
:site => 'https://api.twitter.com',
:authorize_path => '/oauth/authenticate',
:debug_output => false)
def get_request_token(consumer)
request_token = consumer.get_request_token()
return request_token
end
def get_user_authorization(request_token)
puts "Follow this URL to have a user authorize your app: #{request_token.authorize_url()}"
puts "Enter PIN: "
pin = gets.strip
return pin
end
def obtain_access_token(consumer, request_token, pin)
token = request_token.token
token_secret = request_token.secret
hash = { :oauth_token => token, :oauth_token_secret => token_secret }
request_token = OAuth::RequestToken.from_hash(consumer, hash)
# Get access token
access_token = request_token.get_access_token({:oauth_verifier => pin})
return access_token
end
def update_list(url, oauth_params)
options = {
:method => :put,
headers: {
"User-Agent": "v2updateListRuby",
"content-type": "application/json"
},
body: JSON.dump(@json_payload)
}
request = Typhoeus::Request.new(url, options)
oauth_helper = OAuth::Client::Helper.new(request, oauth_params.merge(:request_uri => url))
request.options[:headers].merge!({"Authorization" => oauth_helper.header}) # Signs the request
response = request.run
return response
end
# PIN-based OAuth flow - Step 1
request_token = get_request_token(consumer)
# PIN-based OAuth flow - Step 2
pin = get_user_authorization(request_token)
# PIN-based OAuth flow - Step 3
access_token = obtain_access_token(consumer, request_token, pin)
oauth_params = {:consumer => consumer, :token => access_token}
response = update_list(update_list_url, oauth_params)
puts response.code, JSON.pretty_generate(JSON.parse(response.body))