【问题标题】:Step by step Google SSO (java)?一步一步的谷歌 SSO (java)?
【发布时间】:2012-08-25 02:00:55
【问题描述】:

我在所有打开的用于 Google 单点登录的浏览器选项卡中迷失了 :)

我已经有一个想要在 Google 市场上发布的应用程序。强制集成是 Google SSO。我已经用 Spring 在 Struts2 上构建了应用程序。

所以现在我需要一些说明如何进行这种集成。一个例子将是完美的。或者如何开始,使用哪种技术,最好的方法,类似的东西......

另外,我是否必须使用 Google App Engine 进行 SSO 集成?老实说,我很困惑:)

编辑

我从这里开始:developers.google.com/google-apps/marketplace/sso因为我用的是Java,如果你看最下面的入门,我想用step2,但是链接死了。从那以后我就卡住了……

链接here 也已失效。

【问题讨论】:

  • 你为什么不展示你到目前为止所做的尝试并告诉我们你在哪里卡住了?我已经让 SSO 与 twitter 和 facebook 合作,所以我很确定 google 应该一样简单。
  • 我也用 Facebook 和 Dropbox 做到了,这很容易。但我什至找不到从谷歌开始的地方。我只是感到困惑。我从这里开始:developers.google.com/google-apps/marketplace/sso因为我用的是Java,如果你看最下面的入门,我想用step2,但是链接死了。从那里我被卡住了......
  • 我同意查看该文档非常糟糕。
  • @Trick:我相信你正在寻找这个code.google.com/p/step2

标签: java oauth-2.0 single-sign-on


【解决方案1】:

我用 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);
    }

现在您有了访问令牌,您现在可以访问所有的 Google 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】:

    您可以按照 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

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

    【讨论】:

      猜你喜欢
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-30
      • 1970-01-01
      • 2017-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多