我有一个 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)时,它返回“无效请求”异常
我尝试检查https://developers.google.com/oauthplayground/中的 javascript 函数 onSignIn 中返回的authResult['code']
相同的错误描述显示为“无效请求”。我不确定为什么会这样。返回的错误是“invalid_grant”
任何人都可以解决这个问题吗?我在这里做错了什么?