Example #1
0
  public static UserInfo build(String accessToken, String refreshToken) throws IOException {
    GoogleAccessProtectedResource access =
        new GoogleAccessProtectedResource(
            accessToken,
            ConstFunc.TRANSPORT,
            ConstFunc.JSON_FACTORY,
            ConstFunc.CLIENT_ID,
            ConstFunc.CLIENT_SECRET,
            refreshToken);
    HttpRequestFactory rf = ConstFunc.TRANSPORT.createRequestFactory(access);

    GenericUrl userInfoUrl =
        new GenericUrl("https://www.googleapis.com/oauth2/v1/userinfo?access_token=" + accessToken);

    HttpRequest request = rf.buildGetRequest(userInfoUrl);
    HttpResponse response = request.execute();

    InputStream is = response.getContent();
    Reader isr = new InputStreamReader(is, "UTF-8");

    Gson gson = new Gson();
    UserInfo ui = gson.fromJson(isr, UserInfo.class);

    ui.setAccess_token(accessToken);
    ui.setRefresh_token(refreshToken);

    return ui;
  }
Example #2
0
  @RequestMapping("home")
  public ModelAndView welcomeHandler(HttpServletRequest req, HttpServletResponse res) {
    ModelAndView mv = new ModelAndView("home");

    HttpSession session = req.getSession();
    UserInfo ui = (UserInfo) session.getAttribute("userInfo");

    String accessToken = ui.getAccess_token();

    DocsService service = new DocsService("mayRoro");
    service.setHeader("Authorization", "OAuth " + accessToken);

    try {
      URL metafeedUrl =
          new URL(
              "https://spreadsheets.google.com/feeds/spreadsheets/private/full?title="
                  + ConstFunc.SPREADSHEET_PREFIX);
      SpreadsheetFeed feed = service.getFeed(metafeedUrl, SpreadsheetFeed.class);
      List<SpreadsheetEntry> spreadsheets = feed.getEntries();
      for (int i = 0; i < spreadsheets.size(); i++) {
        SpreadsheetEntry entry = spreadsheets.get(i);
        entry.setTitle(
            new PlainTextConstruct(
                entry.getTitle().getPlainText().substring(ConstFunc.SPREADSHEET_PREFIX.length())));
        System.out.println("Self: " + entry.getSelfLink().getHref());
      }
      mv.addObject("spreadsheets", spreadsheets);
    } catch (Exception e) {
      e.printStackTrace();
    }

    //		************* Seznam vseh map *************
    //		try {
    //			URL metafeedUrl = new URL("https://docs.google.com/feeds/default/private/full/-/folder");
    //			Feed feed = service.getFeed(metafeedUrl, Feed.class);
    //			List<Entry> spreadsheets = feed.getEntries();
    //			for (int i = 0; i < spreadsheets.size(); i++) {
    //				Entry entry = spreadsheets.get(i);
    //				System.out.println("\t" + entry.getTitle().getPlainText());
    //			}
    //			mv.addObject("spreadsheets", spreadsheets);
    //		} catch (Exception e) {
    //			e.printStackTrace();
    //		}

    return mv;
  }
Example #3
0
  @RequestMapping("login")
  public ModelAndView login(HttpServletRequest req, HttpServletResponse res) {
    ModelAndView mv = new ModelAndView("redirect:/home");

    // Check for errors
    String error = req.getParameter("error");
    if (error != null) return new ModelAndView("forward:/error?type=" + error);

    // Get authorization code
    String authorizationCode = req.getParameter("code");

    if (authorizationCode == null)
      return new ModelAndView("forward:/error?type=no_authorization_code");

    // Exchange for an access and refresh token

    GoogleAuthorizationCodeGrant authRequest =
        new GoogleAuthorizationCodeGrant(
            ConstFunc.TRANSPORT,
            ConstFunc.JSON_FACTORY,
            ConstFunc.CLIENT_ID,
            ConstFunc.CLIENT_SECRET,
            authorizationCode,
            ConstFunc.CALLBACK_URL);
    authRequest.useBasicAuthorization = false;
    AccessTokenResponse authResponse = null;
    try {
      authResponse = authRequest.execute();
      if (authResponse.accessToken == null) throw new IOException();
    } catch (IOException e) {
      return new ModelAndView("forward:/error?type=no_access_token");
    }
    String accessToken = authResponse.accessToken;

    System.out.println("accessToken: " + accessToken);
    System.out.println("refreshToken: " + authResponse.refreshToken);

    // Get user data

    UserInfo ui = new UserInfo();
    try {
      ui = UserInfo.build(accessToken, authResponse.refreshToken);
    } catch (IOException e) {
      return new ModelAndView("forward:/error?type=user_info");
    }

    HttpSession session = req.getSession();
    session.setAttribute("userInfo", ui);

    return mv;
  }