forked from xdevplatform/Twitter-API-v2-sample-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete_tweet.rb
79 lines (57 loc) · 2.3 KB
/
delete_tweet.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
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 replace your-tweet-id with the id of the Tweet you wish to delete.
id = "your-tweet-id"
delete_tweet_url = "https://api.twitter.com/2/tweets/#{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 delete_tweet(url, oauth_params)
options = {
:method => :delete,
headers: {
"User-Agent": "v2DeleteTweetRuby",
"content-type": "application/json"
}
}
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 = delete_tweet(delete_tweet_url, oauth_params)