-
Notifications
You must be signed in to change notification settings - Fork 15
/
OltuJavaClient.java
executable file
·89 lines (75 loc) · 3.46 KB
/
OltuJavaClient.java
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
package example.brightcove.oauth.client;
import org.apache.oltu.oauth2.client.OAuthClient;
import org.apache.oltu.oauth2.client.URLConnectionClient;
import org.apache.oltu.oauth2.client.request.OAuthClientRequest;
import org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse;
import org.apache.oltu.oauth2.common.message.types.GrantType;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* Example of the OAuth client credentials flow using the Apache Oltu OAuth2 client.
*/
public class OltuJavaClient {
/**
* URL for requesting OAuth access tokens.
*/
public static final String TOKEN_REQUEST_URL = "https://oauth.brightcove.com/v4/access_token";
/**
* Client ID of your client credential. Change this to match whatever credential you have created.
*/
public static final String CLIENT_ID = "YOUR_CLIENT_ID";
/**
* Client secret of your client credential. Change this to match whatever credential you have created.
*/
public static final String CLIENT_SECRET =
"YOUR_CLIENT_SECRET";
/**
* Account on which you want to request a resource. Change this to match the account you want to
* retrieve resources on.
*/
public static final String ACCOUNT_ID = "YOUR_ACCOUNT_ID";
/**
* URL from which you are going to request a resource. The example below is for the Analytics
* resource server. :account-id will be replaced with {@link ACCOUNT_ID} below.
*/
public static final String RESOURCE_URL_TPL =
"https://analytics.api.brightcove.com/v1/data?accounts=:account-id&dimensions=video";
/**
* Request a fresh access token using the given client ID, client secret, and token request URL,
* then request the resource at the given resource URL using that access token, and get the resource
* content. If an exception is thrown, print the stack trace instead.
*
* @param args Command line arguments are ignored.
*/
public static void main(String[] args) {
try {
OAuthClient client = new OAuthClient(new URLConnectionClient());
OAuthClientRequest request =
OAuthClientRequest.tokenLocation(TOKEN_REQUEST_URL)
.setGrantType(GrantType.CLIENT_CREDENTIALS)
.setClientId(CLIENT_ID)
.setClientSecret(CLIENT_SECRET)
// .setScope() here if you want to set the token scope
.buildQueryMessage();
String token =
client.accessToken(request, OAuthJSONAccessTokenResponse.class)
.getAccessToken();
String resourceUrl = RESOURCE_URL_TPL.replace(":account-id", ACCOUNT_ID);
HttpURLConnection resource_cxn =
(HttpURLConnection)(new URL(resourceUrl).openConnection());
resource_cxn.addRequestProperty("Authorization", "Bearer " + token);
InputStream resource = resource_cxn.getInputStream();
// Do whatever you want to do with the contents of resource at this point.
BufferedReader r = new BufferedReader(new InputStreamReader(resource, "UTF-8"));
String line = null;
while ((line = r.readLine()) != null) {
System.out.println(line);
}
} catch (Exception exn) {
exn.printStackTrace();
}
}
}