Repository navigation
Expand file tree
/
Copy pathoauthtoken.java
More file actions
58 lines (46 loc) · 1.77 KB
/
Copy pathoauthtoken.java
File metadata and controls
58 lines (46 loc) · 1.77 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
import org.codehaus.jettison.json.JSONObject;
import org.joda.time.DateTime;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import com.autodesk.client.auth.OAuth2TwoLegged;
@WebServlet({ "/oauthtoken" })
public class oauthtoken extends HttpServlet {
public oauthtoken() {
}
public void init() throws ServletException {
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setCharacterEncoding("utf8");
resp.setContentType("application/json");
PrintWriter out = resp.getWriter();
JSONObject obj = new JSONObject();
try {
// get oAuth of public, in order to get the token with limited permission
OAuth2TwoLegged forgeOAuth = oauth.getOAuthPublic();
String token = forgeOAuth.getCredentials().getAccessToken();
// this is a timestamp, not the exact value of expires_in, so calculate back
// client side will need this. though not necessary
long expire_time_from_SDK = forgeOAuth.getCredentials().getExpiresAt();
// because we do not know when the token is got, align to current time
// which will be a bit less than what Forge sets (say 3599 seconds). This makes
// sense.
long expires_in = (long) (expire_time_from_SDK - DateTime.now().toDate().getTime()) / 1000;
// send to client
obj.put("access_token", token);
obj.put("expires_in", expires_in);
out.print(obj);
} catch (Exception var2) {
System.out.print("get token exception: " + var2.toString());
resp.setStatus(500);
}
}
public void destroy() {
super.destroy();
}
}