I need login user in server side by access token and get their user infomrmations. it is possible ?
Im looks in com.google.appengine.api but Im not find it.
I get acces token in client side using google oauth2 and after send to server.
我需要通过访问令牌在服务器端登录用户并获取他们的用户信息。有可能的 ?我在com.google.appengine.api中查找,但我找不到它。我使用谷歌oauth2在客户端获得访问令牌,然后发送到服务器。
thanks.
1 个解决方案
#1
0
I found the solution with calling url to google api.
我找到了调用url到google api的解决方案。
public UserInfo login(String token) {
String json = getJsonDataFromUrl("https://www.googleapis.com/oauth2/v2/userinfo?access_token=" + token);
UserInfo userInfo = new UserInfo();
Gson gson = new Gson();
try{
userInfo = gson.fromJson(json, UserInfo.class);
userInfo.logined = true;
HttpSession session = getRequest().getSession();
session.setAttribute("userKey", userInfo.encodedKey);
}catch (Exception ex){
log.log(Level.WARNING, ex.getMessage());
throw ex;
}
return userInfo;
}
private String getJsonDataFromUrl(String aurl){
String json = "";
try {
URL url = new URL(aurl);
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
String line;
while ((line = reader.readLine()) != null) {
json+=line;
}
reader.close();
} catch (MalformedURLException e) {
log.log(Level.SEVERE, e.getMessage());
return null;
} catch (IOException e) {
log.log(Level.SEVERE, e.getMessage());
return null;
}
return json;
}
#1
0
I found the solution with calling url to google api.
我找到了调用url到google api的解决方案。
public UserInfo login(String token) {
String json = getJsonDataFromUrl("https://www.googleapis.com/oauth2/v2/userinfo?access_token=" + token);
UserInfo userInfo = new UserInfo();
Gson gson = new Gson();
try{
userInfo = gson.fromJson(json, UserInfo.class);
userInfo.logined = true;
HttpSession session = getRequest().getSession();
session.setAttribute("userKey", userInfo.encodedKey);
}catch (Exception ex){
log.log(Level.WARNING, ex.getMessage());
throw ex;
}
return userInfo;
}
private String getJsonDataFromUrl(String aurl){
String json = "";
try {
URL url = new URL(aurl);
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
String line;
while ((line = reader.readLine()) != null) {
json+=line;
}
reader.close();
} catch (MalformedURLException e) {
log.log(Level.SEVERE, e.getMessage());
return null;
} catch (IOException e) {
log.log(Level.SEVERE, e.getMessage());
return null;
}
return json;
}