步骤谷歌SSO (java)?

时间:2021-08-17 22:14:19

I am lost in all my open browser tabs for Google single sign on :)

我在谷歌的所有打开的浏览器标签中迷失了:)

I already have an application which I would like to put on Google market place. And mandatory integration is Google SSO. I have built application on Struts2 with Spring.

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

So now I need some instructions how to make this integration. An example would be perfect. Or how to start, which technology to use, best approaches, anything similar...

现在我需要一些说明如何进行积分。一个例子是完美的。或者如何开始,使用哪种技术,最好的方法,任何类似的东西……

Also, do I have to use Google App Engine for SSO integration or no? Honestly, I am confused :)

另外,我是否需要使用谷歌应用程序引擎进行SSO集成?老实说,我很困惑:

EDIT

编辑

I started here: developers.google.com/google-apps/marketplace/sso Because I use Java, if you look at Getting started at the bottom, I wanted to use step2, but the link is dead. From there on I got stuck...

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

Links here are also dead.

这里的链接也没有了。

2 个解决方案

#1


8  

I made with Apache's HttpClient. This is my solution and works perfectly for me.

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

First point to authorization url:

授权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>

https://accounts.google.com/o/oauth2/auth?scope=https:/ /www.googleapis.com/auth/userinfo.profile + https://accounts.google.com/o/oauth2/auth?scope=https < YOUR_CLIENT_ID > &redirect_uri = < YOUR_CALLBACK_URL >

Then get parameters back from your redirect_uri and build request body for getting access_token:

然后从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";

Then with HttpClient make a POST and with simple JSON parser parse out the access token.

然后使用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);
    }

Now you have the access token and you can now access all of the Google API. For example, getting all the users info:

现在有了访问令牌,现在可以访问所有的谷歌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);
    }

This data you can now save and use it for login to your application.

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

#2


3  

You can follow the Java tutorial for the Google Apps Marketplace which explains how to perform Single Sign-On: https://developers.google.com/google-apps/marketplace/tutorial_java

您可以参考谷歌应用程序市场的Java教程,其中解释了如何执行单点登录:https://developers.google.com/google-apps/marketplace/tutorial_java

The tutorial also includes a download link for a zip with the application source and all the required libraries, including step2: http://apps-marketplace-resources.googlecode.com/files/helloworld-java-2011050303.zip

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

Here are valid links for the broken ones in the docs:

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

#1


8  

I made with Apache's HttpClient. This is my solution and works perfectly for me.

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

First point to authorization url:

授权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>

https://accounts.google.com/o/oauth2/auth?scope=https:/ /www.googleapis.com/auth/userinfo.profile + https://accounts.google.com/o/oauth2/auth?scope=https < YOUR_CLIENT_ID > &redirect_uri = < YOUR_CALLBACK_URL >

Then get parameters back from your redirect_uri and build request body for getting access_token:

然后从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";

Then with HttpClient make a POST and with simple JSON parser parse out the access token.

然后使用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);
    }

Now you have the access token and you can now access all of the Google API. For example, getting all the users info:

现在有了访问令牌,现在可以访问所有的谷歌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);
    }

This data you can now save and use it for login to your application.

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

#2


3  

You can follow the Java tutorial for the Google Apps Marketplace which explains how to perform Single Sign-On: https://developers.google.com/google-apps/marketplace/tutorial_java

您可以参考谷歌应用程序市场的Java教程,其中解释了如何执行单点登录:https://developers.google.com/google-apps/marketplace/tutorial_java

The tutorial also includes a download link for a zip with the application source and all the required libraries, including step2: http://apps-marketplace-resources.googlecode.com/files/helloworld-java-2011050303.zip

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

Here are valid links for the broken ones in the docs:

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