-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathMain.java
98 lines (75 loc) · 3.89 KB
/
Main.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
90
91
92
93
94
95
96
97
98
package nl.bigo.retrofitoauth;
import retrofit.RetrofitError;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.Scanner;
public class Main {
private static final String PROPERTIES_FILE_NAME = "local.properties";
private static final String[] MANDATORY_PROPERTIES = { "client_id", "client_secret" };
private static Properties loadPropertiesFile(String fileName) {
Properties properties = null;
try {
properties = new Properties();
properties.load(new FileInputStream(fileName));
}
catch (IOException e) {
System.err.println("something went wrong reading the properties file: " + e.getMessage());
System.exit(1);
}
boolean success = true;
for (String key : MANDATORY_PROPERTIES) {
String value = properties.getProperty(key);
if (value == null || value.trim().isEmpty()) {
System.err.println("missing property: '" + key + "'");
success = false;
}
}
if (!success) {
System.exit(1);
}
return properties;
}
public static void main(String[] args) {
Properties properties = loadPropertiesFile(PROPERTIES_FILE_NAME);
try {
final Scanner keyboard = new Scanner(System.in);
final String clientId = properties.getProperty(MANDATORY_PROPERTIES[0]);
final String clientSecret = properties.getProperty(MANDATORY_PROPERTIES[1]);
GoogleAccountsService accountsService = ServiceGenerator.createService(GoogleAccountsService.class, GoogleAccountsService.BASE_URL);
// First get the code the user will enter in a browser.
UserCode userCode = accountsService.getUserCode(clientId, "email profile");
System.out.println(userCode);
// Give instructions to the user.
System.out.printf("visit '%s' and enter the code: '%s' (press return once this is done)",
userCode.getVerificationUrl(), userCode.getUserCode());
keyboard.nextLine();
// Get the access token after the user verified access and get the user's profile with it.
AccessToken accessToken = accountsService.getAccessToken(clientId, clientSecret,
userCode.getDeviceCode(), GoogleAccountsService.ACCESS_GRANT_TYPE);
// Store this refresh token separately: subsequent access tokens will not include it! Getting
// a new refresh token would mean asking the user to enter another code!
final String refreshToken = accessToken.getRefreshToken();
GoogleApisService apisService = ServiceGenerator.createService(GoogleApisService.class, GoogleApisService.BASE_URL, accessToken);
System.out.println("token: " + accessToken);
System.out.println(apisService.getProfile());
// Now pretend that our access token expired and get a new access token with the refresh token.
AccessToken refreshedAccessToken = accountsService.refreshAccessToken(clientId, clientSecret,
refreshToken, GoogleAccountsService.REFRESH_GRANT_TYPE);
apisService = ServiceGenerator.createService(GoogleApisService.class, GoogleApisService.BASE_URL, refreshedAccessToken);
System.out.println("refreshed token: " + refreshedAccessToken); // Note: no refreshToken in here!
System.out.println(apisService.getProfile());
}
catch (RetrofitError error) {
if (error.getResponse() == null) {
System.out.println("error=" + error);
}
else {
System.out.println("response=" + error.getResponse() + ", status=" + error.getResponse().getStatus());
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}