Google OAuth2本地运行正常,部署至bt.mypage.kr域名后报错
问题描述
本地主机运行Google Calendar集成功能正常,但部署到bt.mypage.kr域名后出现错误,提示找不到对应页面,跳转的授权URL为:
http://bt.mypage.kr/o/oauth2/v2/auth?scope=https://www.googleapis.com/auth/calendar+https://www.googleapis.com/auth/calendar.events&access_type=online&include_granted_scopes=true&response_type=code&state=state_parameter_passthrough_value&redirect_uri=http://bt.mypage.kr/Schedule/UI/oauth2callback&client_id=myclientid
相关代码如下:
public ActionResult AuthRedirect() { var url = "http://" + Request.Host; sting redirect_uri = url + "/Schedule/UI/oauth2callback" GoogleParram gpr = new GoogleParram(url); string rerectUrl = "https://accounts.google.com/o/oauth2/v2/auth?" + "scope=https://www.googleapis.com/auth/calendar+https://www.googleapis.com/auth/calendar.events&" + "access_type=online&" + "include_granted_scopes=true&" + "response_type=code&" + "state=state_parameter_passthrough_value&" + "redirect_uri=" + url + "/Schedule/UI/oauth2callback&" + "client_id=" + gpr.client_id; return Redirect(rerectUrl); } public void oauth2callback(string code, string error, string state) { if (string.IsNullOrWhiteSpace(error)) { this.GetTokens(code); } } public IActionResult GetTokens(string code) { var url = "http://" + Request.Host; GoogleParram gpr = new GoogleParram(url); var request = new RestRequest(); request.AddQueryParameter("client_id", gpr.client_id); request.AddQueryParameter("client_secret", gpr.client_secret); request.AddQueryParameter("code", code); request.AddQueryParameter("grant_type", "authorization_code"); request.AddQueryParameter("redirect_uri", url + "/Schedule/UI/oauth2callback&"); RestClient restClient = new RestClient(gpr.token_uri); var response = restClient.Post(request); if (response.StatusCode == System.Net.HttpStatusCode.OK) { gpr.WriteToken(response.Content); return RedirectToAction("Calendar"); } return RedirectToAction("Calendar"); }
解决方案
1. 修正代码拼写与语法错误
sting redirect_uri拼写错误,改为string redirect_uri,并补上分号:string redirect_uri = url + "/Schedule/UI/oauth2callback";GetTokens方法中,redirect_uri参数末尾多了多余的&,导致参数解析错误,修正为:request.AddQueryParameter("redirect_uri", url + "/Schedule/UI/oauth2callback");
2. 对齐Google控制台的重定向URI配置
登录Google Cloud控制台,找到对应OAuth 2.0客户端ID,检查授权重定向URI列表:
- 必须添加
http://bt.mypage.kr/Schedule/UI/oauth2callback(若部署后网站强制HTTPS,则改为https://bt.mypage.kr/Schedule/UI/oauth2callback) - 重定向URI需与代码中传递的完全一致,包括协议、域名、路径,大小写敏感
3. 动态获取请求协议
避免硬编码HTTP导致部署后协议不匹配,修改基础URL获取逻辑:
var url = $"{Request.Scheme}://{Request.Host}";
4. 编码重定向URI
手动拼接授权URL时,对重定向URI进行URL编码,避免特殊字符解析异常:
string encodedRedirectUri = Uri.EscapeDataString(url + "/Schedule/UI/oauth2callback"); string rerectUrl = "https://accounts.google.com/o/oauth2/v2/auth?" + "scope=https://www.googleapis.com/auth/calendar+https://www.googleapis.com/auth/calendar.events&" + "access_type=online&" + "include_granted_scopes=true&" + "response_type=code&" + "state=state_parameter_passthrough_value&" + "redirect_uri=" + encodedRedirectUri + "&" + "client_id=" + gpr.client_id;
5. 验证回调路由有效性
确认部署后的服务器上,/Schedule/UI/oauth2callback 路由已正确注册,无URL重写、路由前缀等配置导致的访问失败问题。
内容的提问来源于stack exchange,提问作者Hong Van Vit
相关产品推荐
相关产品推荐

