2012-08-30 107 views
12

我迷失在我的所有打开的浏览器标签中,用于Google单一登录:)一步一步Google SSO(java)?

我已经有了一个应用程序,我想将它放在Google市场上。而强制整合是Google SSO。我已经用Spring构建了Struts2上的应用程序。

所以现在我需要一些说明如何使这种整合。一个例子是完美的。或者如何开始,使用哪种技术,哪种技术最好,什么类似...

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

编辑

我开始在这里:developers.google.com/google-apps/marketplace/sso因为我使用Java,如果你看看底部入门,我想用第二步,但链接已经死了。从那里我卡住了...

链接here也死了。

+0

为什么你不显示你到目前为止尝试过的东西,并告诉我们你卡在哪里?我已经通过twitter和facebook获得了SSO,所以我很确定谷歌应该很容易。 – Quaternion

+0

我也是用Facebook和Dropbox做的,那很简单。但我甚至找不到从哪里开始Google。我只是感到困惑。我从这里开始:https://developers.google.com/google-apps/marketplace/sso因为我使用Java,所以如果您查看底部的入门知识,我想使用step2,但链接已死亡。从那里我卡住了... – Trick

+0

我同意看那个文件是非常糟糕的。 – Quaternion

回答

7

我与Apache的HttpClient的制作。这是我的解决方案,适用于我。

第一点授权网址:

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

您现在可以保存并使用该数据登录到您的应用程序。

3

您可以按照对谷歌Apps Marketplace中的Java教程,说明如何进行单点登录:https://developers.google.com/google-apps/marketplace/tutorial_java

本教程还包括下载链接与应用程序源的拉链,所有需要的库,包括第二步:http://apps-marketplace-resources.googlecode.com/files/helloworld-java-2011050303.zip

以下是该文档为破的有效链接: