forked from xdevplatform/Twitter-API-v2-sample-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete_bookmark.rb
111 lines (86 loc) · 3.18 KB
/
delete_bookmark.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
require 'json'
require 'typhoeus'
require 'twitter_oauth2'
# First, you will need to enable OAuth 2.0 in your App’s auth settings in the Developer Portal to get your client ID.
# Inside your terminal you will need to set an enviornment variable
# export CLIENT_ID='your-client-id'
client_id = ENV["CLIENT_ID"]
# If you have selected a type of App that is a confidential client you will need to set a client secret.
# Confidential Clients securely authenticate with the authorization server.
# Inside your terminal you will need to set an enviornment variable
# export CLIENT_SECRET='your-client-secret'
# Remove the comment on the following line if you are using a confidential client
# client_secret = ENV["CLIENT_SECRET"]
# Replace the following URL with your callback URL, which can be obtained from your App's auth settings.
redirect_uri = "https://www.example.com"
# Replace with a Tweet ID you want to remove a Bookmark of
tweet_id = "1460323737035677698"
# Start an OAuth 2.0 session with a public client
client = TwitterOAuth2::Client.new(
identifier: "#{client_id}",
redirect_uri: "#{redirect_uri}"
)
# Start an OAuth 2.0 session with a confidential client
# Remove the comment on the following lines if you are using a confidential client
# client = TwitterOAuth2::Client.new(
# identifier: "#{client_id}",
# secret: "#{client_secret}",
# redirect_uri: "#{redirect_uri}"
# )
# Create your authorize url
authorization_url = client.authorization_uri(
scope: [
:'users.read',
:'tweet.read',
:'bookmark.write',
:'offline.access'
]
)
# Set code verifier and state
code_verifier = client.code_verifier
state = client.state
# Visit the URL to authorize your App to make requests on behalf of a user
print 'Visit the following URL to authorize your App on behalf of your Twitter handle in a browser'
puts authorization_url
`open "#{authorization_url}"`
print 'Paste in the full URL after you authorized your App: ' and STDOUT.flush
# Fetch your access token
full_text = gets.chop
new_code = full_text.split("code=")
code = new_code[1]
client.authorization_code = code
# Your access token
token_response = client.access_token! code_verifier
# Make a request to the users/me endpoint to get your user ID
def users_me(url, token_response)
options = {
method: 'get',
headers: {
"User-Agent": "BookmarksSampleCode",
"Authorization": "Bearer #{token_response}"
},
}
request = Typhoeus::Request.new(url, options)
response = request.run
return response
end
url = "https://api.twitter.com/2/users/me"
me_response = users_me(url, token_response)
json_s = JSON.parse(me_response.body)
user_id = json_s["data"]["id"]
# Make a request to the bookmarks url
bookmarks_url = "https://api.twitter.com/2/users/#{user_id}/bookmarks/#{tweet_id}"
def bookmarked_tweets(bookmarks_url, token_response)
options = {
method: 'delete',
headers: {
"User-Agent": "BookmarksSampleCode",
"Authorization": "Bearer #{token_response}"
}
}
request = Typhoeus::Request.new(bookmarks_url, options)
response = request.run
return response
end
response = bookmarked_tweets(bookmarks_url, token_response)
puts response.code, JSON.pretty_generate(JSON.parse(response.body))