【问题标题】:SocialAuth and GoogleSocialAuth 和谷歌
【发布时间】:2013-03-27 17:10:31
【问题描述】:

我正在构建一个登录模块,您可以在其中使用各种社交网络帐户登录。第一步是 LinkedIn 和 Google。

LinkedIn 部分工作正常,但 Google 部分却不行。我认为问题出在身份验证之前的代码上,因为返回 url 有 openid 参数,而不是 OAuth 参数。这是重定向之前的代码:

HttpSession session = request.getSession();
SocialAuthConfig config = SocialAuthConfig.getDefault();
    try {
        config.load("/oauth_consumer.properties");
    } catch (Exception ex) {
        System.out.println(ex);
    }

    //Create an instance of SocialAuthManager and set config
    SocialAuthManager manager = new SocialAuthManager();
    try {
        manager.setSocialAuthConfig(config);
    } catch (Exception ex) {
        System.out.println(ex);
    }

    //URL of YOUR application which will be called after authentication
    String successUrl = "http://localhost:8080/ProjectName/completelogin.do";
    String url = "";
    try {
        url = manager.getAuthenticationUrl(type, successUrl).toString();
    } catch (Exception ex) {
        System.out.println(ex);
    }

    // Store in session
    session.setAttribute("authManager", manager);

    response.sendRedirect(url);

这是我为 google 准备的 oauth_consumer.properties 部分

www.google.com.consumer_key = 81XXXXXX466.apps.googleusercontent.com
www.google.com.consumer_secret = WN0oXXXXXXXHoCeSocQK
www.google.com.custom_permissions=https://www.googleapis.com/auth/userinfo.profile,https://www.googleapis.com/auth/userinfo.email

我可以强制 Google 使用 OAuth 身份验证而不是 OpenID 身份验证吗?我已经使用另一个使用 OAuth 的框架登录了谷歌,它就像一个魅力。只是现在,由于我想使用几个社交网站,我不想继续重新发明轮子,而只是使用 socialauth...

【问题讨论】:

    标签: java google-oauth socialauth


    【解决方案1】:

    Google OpenId 身份验证端点为https://accounts.google.com/o/openid2/auth,OAuth2 端点为/o/oauth2/auth。您需要将最终用户重定向到 OAuth2 端点,以告知(或“强制”)Google 使用 OAuth2。

    【讨论】:

    • 是的,我知道这一点。问题是 SocialAuth 有一个使用混合策略的谷歌实现。它应该重定向到 oauth2 url,但它没有。我有 Google 和 LinkedIn 的有效登录名,但我无法使用 SocialAuth 库让它工作。甚至linkedin也使用OAuth1,我想要OAuth2。
    • 在旁注中,您是否知道授权服务器的正确 URL 和参数,因为我尝试的方法不起作用。我现在使用 google api 客户端来交换访问令牌的代码,但我想自己调用 url。
    • 根据 Google OAuth2 Login doc developers.google.com/accounts/docs/OAuth2Login#formingtheurl,OAuth2 auth URL 应该是 accounts.google.com/o/oauth2/auth?带有参数 response_type、client_id、redirect_uri、范围和状态。或者您可以转储 google api 客户端生成的 URL(例如通过调试)。
    • 嗯,这就是适合我的 URL。但它返回代码,然后我需要将该代码交换为令牌。但我会尝试从 api 客户端获取 URL :) thx
    【解决方案2】:

    https://github.com/3pillarlabs/socialauth/wiki/Sample-Properties

    如果需要或需要传递额外参数,您可以设置 OAuth 端点(RequestToken URL、Authorization URL 和 AccessToken URL)

    • www.google.com.request_token_url
    • www.google.com.authentication_url
    • www.google.com.access_token_url

    【讨论】:

      猜你喜欢
      • 2012-02-11
      • 2013-05-20
      • 2017-12-02
      • 1970-01-01
      • 2020-07-02
      • 2020-12-11
      • 1970-01-01
      • 1970-01-01
      • 2020-07-25
      相关资源
      最近更新 更多