Facebook Oauth CORS error

和自甴很熟 提交于 2019-12-10 22:37:24

问题


I am using oauth2 to handle a user login via facebook. The error occurs when I call the authorization server in the golang api.

Here is the network error.

Fetch API cannot load https://www.facebook.com/dialog/oauth?client_id=1543358959292867&redirect_u…=email+public_profile&state=mUi4IpdY8yF5TNVVptMNNSn8IbVSZxJXTSEFM8Zg8LM%3D. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

network info (chrome)

Request URL:https://www.facebook.com/dialog/oauth?client_id=1543358959292867&redirect_uri=http%3A%2F%2Flocalhost%3A7001%2FFBLogin%2FCallback&response_type=code&scope=email+public_profile&state=lkmenB4FjNOShUQzL0Gpymi1xsvauaL7TawmUjCyvI4%3D Request Method:GET Status Code:302 Remote Address:[2a03:2880:f003:c1f:face:b00c:0:25de]:443

request headers

:authority:www.facebook.com :method:GET :path:/dialog/oauth?client_id=1543358959292867&redirect_uri=http%3A%2F%2Flocalhost%3A7001%2FFBLogin%2FCallback&response_type=code&scope=email+public_profile&state=KPbwnGWQoI7PHvwhJ_JvZ7RowPthjqpaDTgKVI5NHrM%3D :scheme:https accept:*/* accept-encoding:gzip, deflate, sdch accept-language:en-US,en;q=0.8 origin:null referer:http://localhost:3000/ user-agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.86 Safari/537.36

Javascript function that calls server api.

let loginUrl = "http://localhost:7001/FBLogin" //server url let config = { method: 'GET', mode: 'cors', } fetch(loginUrl, config).then(response => { ... }

Golang function that calls oauth authorization server (facebook).

func FBLogin(w http.ResponseWriter, r *http.Request) { state, err := hashutil.GenerateRandomState() logutil.Fatal(err) url := config["fb"].AuthCodeURL(state) logutil.OauthSessionLogger(state) http.Redirect(w, r, url, 302) }

I set the proper headers on the server side following this guide. Setting HTTP headers in Golang

I can't seem to find where the error comes from. (facebook API developer console, the callback headers in the server code, or the javascript initiating the call? I spent a lot of time changing the server headers but the error still persists as a CORS issue.

The origin in my request header is null, is that something to consider?

edit added no-cors mode result.

FBLogin 302 text/html SessionActions.js?524b:35 736 B 5 ms
oauth?client_id=154335&redirect_uri=http%3A%2F%2Flocalhost%3A7001%2FFBLogin%2FCallback&D 302 text/html
http://localhost:7001/FBLogin 611 B 57 ms
login.php?skip_api_login=1&api_key=154335 200
All three calls were successful, but was never redirected to the oauth page even when it said so. The response type was 'opaque' with a status 0.


回答1:


a network trace using wireshark or tcpdump can help to find what's going on or capture http traffic using any firefox plugin to capture live headers.



来源:https://stackoverflow.com/questions/36924580/facebook-oauth-cors-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!