代码之家  ›  专栏  ›  技术社区  ›  Trick

一步一步谷歌SSO(java)?

  •  13
  • Trick  · 技术社区  · 14 年前

    我在谷歌单一登录的所有打开的浏览器选项卡中都迷路了:)

    我已经有了一个应用程序,我想把它放在谷歌市场上。强制集成是Google SSO。我已经在带有Spring的Struts2上构建了应用程序。

    所以现在我需要一些关于如何进行集成的说明。举个例子就是完美的。或者如何开始,使用哪种技术,最佳方法,任何类似的。。。

    而且 我必须要吗 是否使用谷歌应用程序引擎进行SSO集成?老实说,我很困惑:)

    编辑

    我从这里开始: developers.google.com/google-apps/marketplace/sso 因为我使用Java,如果你看看底部的Getting started,我想使用step2,但链接已经死了。从那以后,我陷入了困境。。。

    链接 here 他们也死了。

    2 回复  |  直到 14 年前
        1
  •  9
  •   Trick    13 年前

    我用Apache的HttpClient制作的。这是我的解决方案,非常适合我。

    首先指向授权url:

    https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/userinfo.profile+https://www.googleapis.com/auth/userinfo.email&state=%2Fprofile&response_type=code&client_id=<YOUR_CLIENT_ID>&redirect_uri=<YOUR_CALLBACK_URL>

    然后从您的 redirect_uri 并构建用于获取 access_token 以下为:

        String code =  request.getParameter("code");
        String foros = "code="+code +
                    "&client_id=<YOUR_CLIENT_ID>" +
                    "&client_secret=<YOUR_CLIENT_SECRET>" +
                    "&redirect_uri="+getText("google.auth.redirect.uri") +
                    "&grant_type=authorization_code";
    

    然后使用HttpClient进行POST,并使用简单的JSON解析器解析出访问令牌。

        HttpClient client = new HttpClient();
        String url = "https://accounts.google.com/o/oauth2/token";
        PostMethod post = new PostMethod(url);
        post.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    
        try {
            post.setRequestEntity(new StringRequestEntity(foros, null, null));
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        String accessToken = null;
        try {
            client.executeMethod(post);
            String resp = post.getResponseBodyAsString();
            JSONParser jsonParser = new JSONParser();
            Object obj = jsonParser.parse(resp);
            JSONObject parsed = (JSONObject)obj;            
            accessToken = (String) parsed.get("access_token");
        } catch (HttpException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    

    现在您有了访问令牌,现在可以访问 全部的 谷歌API。例如,获取所有用户信息:

        GetMethod getUserInfo = new GetMethod("https://www.googleapis.com/oauth2/v1/userinfo?access_token="+accessToken);
    
        String googleId = null;
        String email = null;
        String name = null;
        String firstName = null;
        String lastName = null;
        try {
            client.executeMethod(getUserInfo);
            String resp = getUserInfo.getResponseBodyAsString();
            JSONParser jsonParser = new JSONParser();
            Object obj = jsonParser.parse(resp);
            JSONObject parsed = (JSONObject)obj;
            googleId = (String) parsed.get("id");
            email = (String) parsed.get("email");
            name = (String) parsed.get("name");
            firstName = (String) parsed.get("given_name");
            lastName = (String) parsed.get("family_name");
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    

    您现在可以保存这些数据并将其用于登录到您的应用程序。

        2
  •  5
  •   Claudio Cherubino    14 年前

    您可以遵循Google Apps Marketplace的Java教程,该教程解释了如何执行单点登录: https://developers.google.com/google-apps/marketplace/tutorial_java

    本教程还包括一个zip的下载链接,其中包含应用程序源和所有必需的库,包括步骤2: http://apps-marketplace-resources.googlecode.com/files/helloworld-java-2011050303.zip

    以下是文档中损坏的链接的有效链接:

    推荐文章