【问题标题】:Google OAuth Issue谷歌 OAuth 问题
【发布时间】:2017-05-22 16:48:09
【问题描述】:

我有一个 Umbraco 网站,其 google 登录按钮配置如下:

在页面顶部(在标题部分内)我有调用 google API 的脚本:

<script src="https://apis.google.com/js/client:platform.js?onload=start" async defer></script>
<script>
        function start() {
          gapi.load('auth2', function() {
            auth2 = gapi.auth2.init({
              client_id: '<myapp client Id>.apps.googleusercontent.com',
              // Scopes to request in addition to 'profile' and 'email'
              redirect_uri: 'http://localhost:40136/umbraco/Surface/AuthSurface/GoogleAuthrizedUser',
              scope: 'profile email'
            });
          });
        } 
 </script>

在代码的正文部分,我设置了谷歌按钮和相关的点击功能:

<script>
 function onSignIn(authResult) {
    if (authResult['code']) {
      var authCode = authResult['code'];
      console.log("Authorization Code: " + authCode);
      $.post("/umbraco/Surface/AuthSurface/GoogleAuthrizedUser", { code: authCode })
        .done(function(msg) {
            // Success settings
         })
        .fail(function(xhr, status, error) {

        });
    } else {
       //authResult['code'] is null
       //handle the error message.
    }
  };
 </script>

处理服务器端回调的控制器代码:

  public class AuthSurfaceController : SurfaceController
  {
       public ActionResult GoogleAuthrizedUser()
      {
        string AuthCode = HttpContext.Request["code"];
        var info = new GoogleAccessTokenResponse();
        var client = new GoogleOAuthClient();
        try
        {
          info = client.GetAccessTokenFromAuthorizationCode(AuthCode);
        }
        catch (Exception ex)
        {
          var strMessage = String.Format("<div class=\"info\"><p>{0}</p><p>{1}</p></div>", "Google Login Error",
          ex.Message);
         return Json(new AjaxOperationResponse(false, strMessage));
        }
     }
 }

在服务器端,我使用 Skybrud Social 插件来访问 google api。

谷歌身份验证发生在弹出窗口中,并使用凭据授权客户端,authResult['code'] 具有有效代码。

在控制器中,当我初始化客户端并调用函数GetAccessTokenFromAuthorizationCode(AuthCode)时,它返回一个异常'Invalid Request'

我尝试检查 https://developers.google.com/oauthplayground/ 中的 javascript 函数 onSignIn 中返回的 authResult['code']

同样的错误描述显示为“无效请求”。我不确定为什么会这样。返回的错误是“invalid_grant”

谁能解决这个问题?我在这里做错了什么?

【问题讨论】:

    标签: umbraco oauth2-playground


    【解决方案1】:

    在您的表面控制器中,您正在初始化GoogleOAuthClient 的新实例,但没有设置任何属性。 GetAccessTokenFromAuthorizationCode 方法要求 ClientIdClientSecretRedirectUri 属性具有值。您可以像这样初始化属性:

    // Initialize a new instance of the OAuth client
    GoogleOAuthClient oauth = new GoogleOAuthClient {
        ClientId = "The client ID of your project",
        ClientSecret = "The client secret of your project",
        RedirectUri = "The return URI (where users should be redirected after the login)"
    };
    

    您可以在文档中阅读有关身份验证的更多信息:http://social.skybrud.dk/google/authentication/(其中解释的方法不会使用任何 JavaScript)

    【讨论】:

    • @abjemer:我已经尝试过这种方法。在调用“GetAccessTokenFromAuthrizationCode”方法之前,已使用“ClientId”、“ClientSecret”和“RedirectUri”初始化了“GoogleOAuthClient”。
    • @abjemer:我已经尝试过这种方法。在调用“GetAccessTokenFromAuthrizationCode”方法之前,已使用“ClientId”、“ClientSecret”和“RedirectUri”初始化了“GoogleOAuthClient”。但是从处理弹出窗口以进行身份​​验证、授权但提供脱机范围访问并返回 authResult['code'] 的 javascript 中取消异步回调,如我上面的查询中所述。想使用这里解释的方法developers.google.com/identity/sign-in/web/server-side-flow。不希望使用 Response.Redirect(authorizationUrl)
    • @abjemer 我们的网站 google 身份验证和授权正在从菜单弹出窗口中进行。因此,重定向到不同的页面将失去异步 javascript 为登录工作流带来的流畅用户体验。
    猜你喜欢
    • 1970-01-01
    • 2019-03-17
    • 2015-10-25
    • 2020-11-27
    • 2014-05-22
    • 1970-01-01
    • 1970-01-01
    • 2016-11-10
    • 2015-05-17
    相关资源
    最近更新 更多