我对 Google 的 oAuth2 api 进行了 AJAX 调用,如下所示:$(document).on('click', '#signup', function() { var OAUTHURL = 'https://accounts.google.com/o/oauth2/auth?'; var SCOPE = 'email profile'; var STATE = 'profile'; var REDIRECT_URI = 'https://localhost:8080/callback'; var RESPONSE_TYPE = 'token'; var CLIENT_ID = '554886359117-8icq0dc9halr8rjd6bdtqcmagrdql9lr.apps.googleusercontent.com'; var _url = OAUTHURL + 'scope=' + SCOPE + '&state=' + STATE + '&redirect_uri=' + REDIRECT_URI + '&response_type=' + RESPONSE_TYPE + '&client_id=' + CLIENT_ID; $.ajax({ type: "POST", dataType: "text", url: _url, success: function(response) { console.log(response.url); }, error: function(error) { console.log("ERROR: ", error); } });});这应该重定向回在http://localhost/callback上运行的服务器。Redirect URIs: http://localhost:8080/callbackJavascript Origins: http://localhost:8080我还有如下定义的回调函数:func init() { r := mux.NewRouter() r.HandleFunc("/", rootHandler) r.HandleFunc("/callback", callbackHandler) http.Handle("/", r)}func callbackHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "I am sent back from the server!"+time.Now().Format("Mon, 02 Jan 2006 15:04:05 MST"))}在调试模式下一切看起来都很好,除了我从谷歌的 api 得到这个错误:XMLHttpRequest 无法加载 https://accounts.google.com/o/oauth2/auth?scope=email%20profile&state=profi …d=554886359117-8icq0dc9halr8rjd6bdtqcmagrdql9lr.apps.googleusercontent.com。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许访问Origin ' http://localhost:8080 '。我已经稍微调整了一下,但我似乎无法找到自己的出路。对此有什么想法吗?
2 回答
森林海
TA贡献2011条经验 获得超2个赞
只需将 CORS 标头添加到您的 callbackHandler 中:
// make this configurable via command line flags, env var, or something
var MyServerName = "https://localhost:8080"
func callbackHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Access-Control-Allow-Origin", MyServerName)
fmt.Fprint(w, "I am sent back from the server!"+time.Now().Format("Mon, 02 Jan 2006 15:04:05 MST"))
}
- 2 回答
- 0 关注
- 202 浏览
添加回答
举报
0/150
提交
取消