forked from xdevplatform/twitter-api-java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOAuth20RefreshToken.java
More file actions
106 lines (91 loc) · 3.5 KB
/
OAuth20RefreshToken.java
File metadata and controls
106 lines (91 loc) · 3.5 KB
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
/*
Copyright 2020 Twitter, Inc.
SPDX-License-Identifier: Apache-2.0
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
https://openapi-generator.tech
Do not edit the class manually.
*/
package com.twitter.clientlib.auth;
import java.util.HashSet;
import java.util.Set;
import com.github.scribejava.core.model.OAuth2AccessToken;
import com.twitter.clientlib.ApiClientCallback;
import com.twitter.clientlib.ApiException;
import com.twitter.clientlib.TwitterCredentialsOAuth2;
import com.twitter.clientlib.api.TwitterApi;
import com.twitter.clientlib.model.ResourceUnauthorizedProblem;
import com.twitter.clientlib.model.Get2TweetsIdResponse;
/**
* An example of how to refresh OAuth2 access token.
*
* Example steps:
* 1. Set `OAuth2` credentials.
* 2. Add the `ApiClientCallback` callback.
* 3. Call `refreshToken()` which will:
* a. Update the user token details inside `TwitterApi`.
* b. Execute the `callback.onAfterRefreshToken()`
* 4. Call the API.
*/
public class OAuth20RefreshToken {
public static void main(String[] args) {
TwitterApi apiInstance = new TwitterApi(new TwitterCredentialsOAuth2(System.getenv("TWITTER_OAUTH2_CLIENT_ID"),
System.getenv("TWITTER_OAUTH2_CLIENT_SECRET"),
System.getenv("TWITTER_OAUTH2_ACCESS_TOKEN"),
System.getenv("TWITTER_OAUTH2_REFRESH_TOKEN")));
apiInstance.addCallback(new MaintainToken());
try {
apiInstance.refreshToken();
} catch (Exception e) {
System.err.println("Error while trying to refresh existing token : " + e);
e.printStackTrace();
return;
}
callApi(apiInstance);
}
public static void callApi(TwitterApi apiInstance) {
Set<String> tweetFields = new HashSet<>();
tweetFields.add("author_id");
tweetFields.add("id");
tweetFields.add("created_at");
try {
// findTweetById
Get2TweetsIdResponse result = apiInstance.tweets().findTweetById("20")
.tweetFields(tweetFields)
.execute();
if (result.getErrors() != null && result.getErrors().size() > 0) {
System.out.println("Error:");
result.getErrors().forEach(e -> {
System.out.println(e.toString());
if (e instanceof ResourceUnauthorizedProblem) {
System.out.println(e.getTitle() + " " + e.getDetail());
}
});
} else {
System.out.println("findTweetById - Tweet Text: " + result.toString());
}
} catch (ApiException e) {
System.err.println("Status code: " + e.getCode());
System.err.println("Reason: " + e.getResponseBody());
System.err.println("Response headers: " + e.getResponseHeaders());
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
class MaintainToken implements ApiClientCallback {
@Override
public void onAfterRefreshToken(OAuth2AccessToken accessToken) {
System.out.println("access: " + accessToken.getAccessToken());
System.out.println("refresh: " + accessToken.getRefreshToken());
}
}