use of com.google.api.client.auth.oauth2.AuthorizationCodeFlow.Builder in project ezyfox-examples by tvd12.
the class GoogleService method getUserInfoByAccessToken.
@Override
public Userinfo getUserInfoByAccessToken(String accessTokenStr) {
Credential credential = new Credential(BearerToken.authorizationHeaderAccessMethod()).setAccessToken(accessTokenStr);
Oauth2 oauth2 = new Oauth2.Builder(new NetHttpTransport(), new GsonFactory(), credential).setApplicationName("Oauth2").build();
try {
return oauth2.userinfo().get().execute();
} catch (Exception e) {
logger.info("get google user information by access token error", e);
return null;
}
}
use of com.google.api.client.auth.oauth2.AuthorizationCodeFlow.Builder in project bible-online by m0ver.
the class login method oAuth2callback.
public String oAuth2callback() throws ApplicationException {
HttpServletRequest request = (HttpServletRequest) this.context.getAttribute(HTTP_REQUEST);
HttpServletResponse response = (HttpServletResponse) this.context.getAttribute(HTTP_RESPONSE);
Reforward reforward = new Reforward(request, response);
TokenResponse oauth2_response;
try {
if (this.getVariable("google_client_secrets") == null) {
clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(login.class.getResourceAsStream("/clients_secrets.json")));
if (clientSecrets.getDetails().getClientId().startsWith("Enter") || clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) {
System.out.println("Enter Client ID and Secret from https://code.google.com/apis/console/ ");
}
this.setVariable(new ObjectVariable("google_client_secrets", clientSecrets), false);
} else
clientSecrets = (GoogleClientSecrets) this.getVariable("google_client_secrets").getValue();
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(GoogleNetHttpTransport.newTrustedTransport(), JSON_FACTORY, clientSecrets, SCOPES).build();
oauth2_response = flow.newTokenRequest(request.getParameter("code")).setRedirectUri(this.getLink("oauth2callback")).execute();
System.out.println("Ok:" + oauth2_response.toString());
} catch (IOException e1) {
// TODO Auto-generated catch block
throw new ApplicationException(e1.getMessage(), e1);
} catch (GeneralSecurityException e) {
// TODO Auto-generated catch block
throw new ApplicationException(e.getMessage(), e);
}
try {
HttpClient httpClient = new DefaultHttpClient();
String url = "https://www.google.com/m8/feeds/contacts/default/full";
url = "https://www.googleapis.com/oauth2/v1/userinfo";
HttpGet httpget = new HttpGet(url + "?access_token=" + oauth2_response.getAccessToken());
httpClient.getParams().setParameter(HttpProtocolParams.HTTP_CONTENT_CHARSET, "UTF-8");
HttpResponse http_response = httpClient.execute(httpget);
HeaderIterator iterator = http_response.headerIterator();
while (iterator.hasNext()) {
Header next = iterator.nextHeader();
System.out.println(next.getName() + ":" + next.getValue());
}
com.google.api.client.http.HttpTransport h;
InputStream instream = http_response.getEntity().getContent();
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
int len;
while ((len = instream.read(bytes)) != -1) {
out.write(bytes, 0, len);
}
instream.close();
out.close();
Struct struct = new Builder();
struct.parse(new String(out.toByteArray(), "utf-8"));
this.usr = new User();
this.usr.setEmail(struct.toData().getFieldInfo("email").stringValue());
if (this.usr.findOneByKey("email", this.usr.getEmail()).size() == 0) {
usr.setPassword("");
usr.setUsername(usr.getEmail());
usr.setLastloginIP(request.getRemoteAddr());
usr.setLastloginTime(new Date());
usr.setRegistrationTime(new Date());
usr.append();
}
new passport(request, response, "waslogined").setLoginAsUser(this.usr.getId());
reforward.setDefault(URLDecoder.decode(this.getVariable("from").getValue().toString(), "utf8"));
reforward.forward();
return new String(out.toByteArray(), "utf-8");
} catch (ClientProtocolException e) {
throw new ApplicationException(e.getMessage(), e);
} catch (IOException e) {
throw new ApplicationException(e.getMessage(), e);
} catch (ParseException e) {
throw new ApplicationException(e.getMessage(), e);
}
}
Aggregations