FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
samples/java/posts/TweetsDemo.java at main · xdevplatform/samples · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
xdevplatform
/
samples
Public
Notifications
You must be signed in to change notification settings
Fork
1.1k
Star
3.2k
Code
Issues
68
Pull requests
39
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
samples
/
java
/
posts
/
TweetsDemo.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
65 lines (56 loc) · 2.46 KB
Breadcrumbs
samples
/
java
/
posts
/
TweetsDemo.java
Copy path
File metadata and controls
65 lines (56 loc) · 2.46 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
org
.
apache
.
http
.
HttpEntity
;
import
org
.
apache
.
http
.
HttpResponse
;
import
org
.
apache
.
http
.
NameValuePair
;
import
org
.
apache
.
http
.
client
.
HttpClient
;
import
org
.
apache
.
http
.
client
.
config
.
CookieSpecs
;
import
org
.
apache
.
http
.
client
.
config
.
RequestConfig
;
import
org
.
apache
.
http
.
client
.
methods
.
HttpGet
;
import
org
.
apache
.
http
.
client
.
utils
.
URIBuilder
;
import
org
.
apache
.
http
.
impl
.
client
.
HttpClients
;
import
org
.
apache
.
http
.
message
.
BasicNameValuePair
;
import
org
.
apache
.
http
.
util
.
EntityUtils
;
import
java
.
io
.
IOException
;
import
java
.
net
.
URISyntaxException
;
import
java
.
util
.
ArrayList
;
/*
* Sample code to demonstrate the use of the v2 Tweets endpoint
* */
public
class
TweetsDemo
{
// To set your enviornment variables in your terminal run the following line:
// export 'BEARER_TOKEN'='<your_bearer_token>'
public
static
void
main
(
String
args
[])
throws
IOException
,
URISyntaxException
{
String
bearerToken
=
System
.
getenv
(
"BEARER_TOKEN"
);
if
(
null
!=
bearerToken
) {
//Replace comma separated ids with Tweets Ids of your choice
String
response
=
getTweets
(
"1138505981460193280,1261326399320715264"
,
bearerToken
);
System
.
out
.
println
(
response
);
}
else
{
System
.
out
.
println
(
"There was a problem getting you bearer token. Please make sure you set the BEARER_TOKEN environment variable"
);
}
}
/*
* This method calls the v2 Tweets endpoint with ids as query parameter
* */
private
static
String
getTweets
(
String
ids
,
String
bearerToken
)
throws
IOException
,
URISyntaxException
{
String
tweetResponse
=
null
;
HttpClient
httpClient
=
HttpClients
.
custom
()
.
setDefaultRequestConfig
(
RequestConfig
.
custom
()
.
setCookieSpec
(
CookieSpecs
.
STANDARD
).
build
())
.
build
();
URIBuilder
uriBuilder
=
new
URIBuilder
(
"https://api.x.com/2/tweets"
);
ArrayList
<
NameValuePair
>
queryParameters
;
queryParameters
=
new
ArrayList
<>();
queryParameters
.
add
(
new
BasicNameValuePair
(
"ids"
,
ids
));
queryParameters
.
add
(
new
BasicNameValuePair
(
"tweet.fields"
,
"created_at"
));
uriBuilder
.
addParameters
(
queryParameters
);
HttpGet
httpGet
=
new
HttpGet
(
uriBuilder
.
build
());
httpGet
.
setHeader
(
"Authorization"
,
String
.
format
(
"Bearer %s"
,
bearerToken
));
httpGet
.
setHeader
(
"Content-Type"
,
"application/json"
);
HttpResponse
response
=
httpClient
.
execute
(
httpGet
);
HttpEntity
entity
=
response
.
getEntity
();
if
(
null
!=
entity
) {
tweetResponse
=
EntityUtils
.
toString
(
entity
,
"UTF-8"
);
}
return
tweetResponse
;
}
}
Back
|
FazBrowse Home
|
New Git URL