Skip to content

Commit aa131cf

Browse files
author
Jessica Garson
committed
adding ruby samples
1 parent b965e61 commit aa131cf

File tree

3 files changed

+334
-0
lines changed

3 files changed

+334
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
require 'json'
2+
require 'typhoeus'
3+
require 'twitter_oauth2'
4+
5+
# First, you will need to enable OAuth 2.0 in your App’s auth settings in the Developer Portal to get your client ID.
6+
# Inside your terminal you will need to set an enviornment variable
7+
# export CLIENT_ID='your-client-id'
8+
client_id = ENV["CLIENT_ID"]
9+
10+
# If you have selected a type of App that is a confidential client you will need to set a client secret.
11+
# Confidential Clients securely authenticate with the authorization server.
12+
13+
# Inside your terminal you will need to set an enviornment variable
14+
# export CLIENT_SECRET='your-client-secret'
15+
16+
# Remove the comment on the following line if you are using a confidential client
17+
# client_secret = ENV["CLIENT_SECRET"]
18+
19+
20+
# Replace the following URL with your callback URL, which can be obtained from your App's auth settings.
21+
redirect_uri = "https://www.example.com"
22+
23+
# Start an OAuth 2.0 session with a public client
24+
client = TwitterOAuth2::Client.new(
25+
identifier: "#{client_id}",
26+
redirect_uri: "#{redirect_uri}"
27+
)
28+
29+
# Start an OAuth 2.0 session with a confidential client
30+
31+
# Remove the comment on the following lines if you are using a confidential client
32+
# client = TwitterOAuth2::Client.new(
33+
# identifier: "#{client_id}",
34+
# secret: "#{client_secret}",
35+
# redirect_uri: "#{redirect_uri}"
36+
# )
37+
38+
# Create your authorize url
39+
authorization_url = client.authorization_uri(
40+
# Update scopes if needed
41+
scope: [
42+
:'users.read',
43+
:'tweet.read',
44+
:'bookmark.read',
45+
:'offline.access'
46+
]
47+
)
48+
49+
# Set code verifier and state
50+
code_verifier = client.code_verifier
51+
state = client.state
52+
53+
# Visit the URL to authorize your App to make requests on behalf of a user
54+
print 'Visit the following URL to authorize your App on behalf of your Twitter handle in a browser'
55+
puts authorization_url
56+
`open "#{authorization_url}"`
57+
58+
print 'Paste in the full URL after you authorized your App: ' and STDOUT.flush
59+
60+
# Fetch your access token
61+
full_text = gets.chop
62+
new_code = full_text.split("code=")
63+
code = new_code[1]
64+
client.authorization_code = code
65+
66+
# Your access token
67+
token_response = client.access_token! code_verifier
68+
69+
# Make a request to the users/me endpoint to get your user ID
70+
def users_me(url, token_response)
71+
options = {
72+
method: 'get',
73+
headers: {
74+
"User-Agent": "BookmarksSampleCode",
75+
"Authorization": "Bearer #{token_response}"
76+
},
77+
}
78+
79+
request = Typhoeus::Request.new(url, options)
80+
response = request.run
81+
82+
return response
83+
end
84+
85+
url = "https://api.twitter.com/2/users/me"
86+
me_response = users_me(url, token_response)
87+
88+
json_s = JSON.parse(me_response.body)
89+
user_id = json_s["data"]["id"]
90+
91+
# Make a request to the bookmarks url
92+
bookmarks_url = "https://api.twitter.com/2/users/#{user_id}/bookmarks"
93+
94+
def bookmarked_tweets(bookmarks_url, token_response)
95+
options = {
96+
method: 'get',
97+
headers: {
98+
"User-Agent": "BookmarksSampleCode",
99+
"Authorization": "Bearer #{token_response}"
100+
}
101+
}
102+
103+
request = Typhoeus::Request.new(bookmarks_url, options)
104+
response = request.run
105+
106+
return response
107+
end
108+
109+
response = bookmarked_tweets(bookmarks_url, token_response)
110+
puts response.code, JSON.pretty_generate(JSON.parse(response.body))
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
require 'json'
2+
require 'typhoeus'
3+
require 'twitter_oauth2'
4+
5+
# First, you will need to enable OAuth 2.0 in your App’s auth settings in the Developer Portal to get your client ID.
6+
# Inside your terminal you will need to set an enviornment variable
7+
# export CLIENT_ID='your-client-id'
8+
client_id = ENV["CLIENT_ID"]
9+
10+
# If you have selected a type of App that is a confidential client you will need to set a client secret.
11+
# Confidential Clients securely authenticate with the authorization server.
12+
13+
# Inside your terminal you will need to set an enviornment variable
14+
# export CLIENT_SECRET='your-client-secret'
15+
16+
# Remove the comment on the following line if you are using a confidential client
17+
# client_secret = ENV["CLIENT_SECRET"]
18+
19+
20+
# Replace the following URL with your callback URL, which can be obtained from your App's auth settings.
21+
redirect_uri = "https://www.example.com"
22+
23+
# Replace with a Tweet ID you want to Bookmark
24+
@json_payload = {"tweet_id": "1460323737035677698"}
25+
26+
# Start an OAuth 2.0 session with a public client
27+
client = TwitterOAuth2::Client.new(
28+
identifier: "#{client_id}",
29+
redirect_uri: "#{redirect_uri}"
30+
)
31+
32+
# Start an OAuth 2.0 session with a confidential client
33+
# Remove the comment on the following lines if you are using a confidential client
34+
# client = TwitterOAuth2::Client.new(
35+
# identifier: "#{client_id}",
36+
# secret: "#{client_secret}",
37+
# redirect_uri: "#{redirect_uri}"
38+
# )
39+
40+
# Create your authorize url
41+
authorization_url = client.authorization_uri(
42+
scope: [
43+
:'users.read',
44+
:'tweet.read',
45+
:'bookmark.write',
46+
:'offline.access'
47+
]
48+
)
49+
50+
# Set code verifier and state
51+
code_verifier = client.code_verifier
52+
state = client.state
53+
54+
# Visit the URL to authorize your App to make requests on behalf of a user
55+
print 'Visit the following URL to authorize your App on behalf of your Twitter handle in a browser'
56+
puts authorization_url
57+
`open "#{authorization_url}"`
58+
59+
print 'Paste in the full URL after you authorized your App: ' and STDOUT.flush
60+
61+
# Fetch your access token
62+
full_text = gets.chop
63+
new_code = full_text.split("code=")
64+
code = new_code[1]
65+
client.authorization_code = code
66+
67+
# Your access token
68+
token_response = client.access_token! code_verifier
69+
70+
# Make a request to the users/me endpoint to get your user ID
71+
def users_me(url, token_response)
72+
options = {
73+
method: 'get',
74+
headers: {
75+
"User-Agent": "BookmarksSampleCode",
76+
"Authorization": "Bearer #{token_response}"
77+
},
78+
}
79+
80+
request = Typhoeus::Request.new(url, options)
81+
response = request.run
82+
83+
return response
84+
end
85+
86+
url = "https://api.twitter.com/2/users/me"
87+
me_response = users_me(url, token_response)
88+
89+
json_s = JSON.parse(me_response.body)
90+
user_id = json_s["data"]["id"]
91+
92+
# Make a request to the bookmarks url
93+
bookmarks_url = "https://api.twitter.com/2/users/#{user_id}/bookmarks"
94+
95+
def bookmarked_tweets(bookmarks_url, token_response)
96+
options = {
97+
method: 'post',
98+
headers: {
99+
"User-Agent": "BookmarksSampleCode",
100+
"content-type": "application/json",
101+
"Authorization": "Bearer #{token_response}"
102+
},
103+
body: JSON.dump(@json_payload)
104+
}
105+
106+
request = Typhoeus::Request.new(bookmarks_url, options)
107+
response = request.run
108+
109+
return response
110+
end
111+
112+
response = bookmarked_tweets(bookmarks_url, token_response)
113+
puts response.code, JSON.pretty_generate(JSON.parse(response.body))
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
require 'json'
2+
require 'typhoeus'
3+
require 'twitter_oauth2'
4+
5+
# First, you will need to enable OAuth 2.0 in your App’s auth settings in the Developer Portal to get your client ID.
6+
# Inside your terminal you will need to set an enviornment variable
7+
# export CLIENT_ID='your-client-id'
8+
client_id = ENV["CLIENT_ID"]
9+
10+
# If you have selected a type of App that is a confidential client you will need to set a client secret.
11+
# Confidential Clients securely authenticate with the authorization server.
12+
13+
# Inside your terminal you will need to set an enviornment variable
14+
# export CLIENT_SECRET='your-client-secret'
15+
16+
# Remove the comment on the following line if you are using a confidential client
17+
# client_secret = ENV["CLIENT_SECRET"]
18+
19+
20+
# Replace the following URL with your callback URL, which can be obtained from your App's auth settings.
21+
redirect_uri = "https://www.example.com"
22+
23+
# Replace with a Tweet ID you want to remove a Bookmark of
24+
tweet_id = "1460323737035677698"
25+
26+
# Start an OAuth 2.0 session with a public client
27+
client = TwitterOAuth2::Client.new(
28+
identifier: "#{client_id}",
29+
redirect_uri: "#{redirect_uri}"
30+
)
31+
32+
# Start an OAuth 2.0 session with a confidential client
33+
# Remove the comment on the following lines if you are using a confidential client
34+
# client = TwitterOAuth2::Client.new(
35+
# identifier: "#{client_id}",
36+
# secret: "#{client_secret}",
37+
# redirect_uri: "#{redirect_uri}"
38+
# )
39+
40+
# Create your authorize url
41+
authorization_url = client.authorization_uri(
42+
scope: [
43+
:'users.read',
44+
:'tweet.read',
45+
:'bookmark.write',
46+
:'offline.access'
47+
]
48+
)
49+
50+
# Set code verifier and state
51+
code_verifier = client.code_verifier
52+
state = client.state
53+
54+
# Visit the URL to authorize your App to make requests on behalf of a user
55+
print 'Visit the following URL to authorize your App on behalf of your Twitter handle in a browser'
56+
puts authorization_url
57+
`open "#{authorization_url}"`
58+
59+
print 'Paste in the full URL after you authorized your App: ' and STDOUT.flush
60+
61+
# Fetch your access token
62+
full_text = gets.chop
63+
new_code = full_text.split("code=")
64+
code = new_code[1]
65+
client.authorization_code = code
66+
67+
# Your access token
68+
token_response = client.access_token! code_verifier
69+
70+
# Make a request to the users/me endpoint to get your user ID
71+
def users_me(url, token_response)
72+
options = {
73+
method: 'get',
74+
headers: {
75+
"User-Agent": "BookmarksSampleCode",
76+
"Authorization": "Bearer #{token_response}"
77+
},
78+
}
79+
80+
request = Typhoeus::Request.new(url, options)
81+
response = request.run
82+
83+
return response
84+
end
85+
86+
url = "https://api.twitter.com/2/users/me"
87+
me_response = users_me(url, token_response)
88+
89+
json_s = JSON.parse(me_response.body)
90+
user_id = json_s["data"]["id"]
91+
92+
# Make a request to the bookmarks url
93+
bookmarks_url = "https://api.twitter.com/2/users/#{user_id}/bookmarks/#{tweet_id}"
94+
95+
def bookmarked_tweets(bookmarks_url, token_response)
96+
options = {
97+
method: 'delete',
98+
headers: {
99+
"User-Agent": "BookmarksSampleCode",
100+
"Authorization": "Bearer #{token_response}"
101+
}
102+
}
103+
104+
request = Typhoeus::Request.new(bookmarks_url, options)
105+
response = request.run
106+
107+
return response
108+
end
109+
110+
response = bookmarked_tweets(bookmarks_url, token_response)
111+
puts response.code, JSON.pretty_generate(JSON.parse(response.body))

0 commit comments

Comments
 (0)