在前端调用此API(在托管服务器上)时,该错误将在那时重定向到Google OAuth以供Google登录。
从CORS策略阻止从原点“ https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=114887XXXXX7-rsXXXXXXXXXXXXXXXXXXXrg.apps.googleusercontent.com&redirect_uri=https%3A%2F%2Fpricemonitor.info%2Fyoutube%2Foauth2callback&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube.force-ssl&state=bmZbXXXXXXXXXXXXXXXXXXXXFS&access_type=offline&include_granted_scopes=true”到“ https://pricemonitor.info/youtube”(从“ https://pricemonitor.info”重定向)的访问:对预检请求的响应未通过访问控件检查:所请求的资源上不存在“ Access-Control-Allow-Origin”标头。如果不透明的响应满足您的需求,请将请求的模式设置为“ no-cors”以在禁用CORS的情况下获取资源。
我使用的环境是:
后端-Python
前端-reactjs
网络服务 r- WEBHOSTPYTHON
这里我添加了用于泳镜oAuthentication的服务器端代码
@app.route('/oauth2callback',methods=['GET', 'POST'])
@cross_origin(origins='*',allow_headers=['Access-Control-Allow-
Origin'])
def oauth2callback():
# Specify the state when creating the flow in the callback so that
it can
# verify the authorization server response.
state = flask.session['state']
flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
CLIENT_SECRETS_FILE, scopes=SCOPES, state=state)
flow.redirect_uri = flask.url_for('oauth2callback',
_external=True)
# Use the authorization server's response to fetch the OAuth 2.0
tokens.
authorization_response = flask.request.url
#authorization_response = flask.request.headers['Access-Control-
Allow-Origin'] = '*'
authorization_response = flask.Response.headers.set['Access-
Control-Allow-Origin'] = '*'
authorization_response = authorization_response.replace('http',
'https')
client_secret = 'lDXXXXXXXXXXXXXXXXZoK'
flow.fetch_token(client_secret=client_secret,
authorization_response=authorization_response)
# Store the credentials in the session.
# ACTION ITEM for developers:
# Store user's access and refresh tokens in your data store if
# incorporating this code into your real app.
credentials = flow.credentials
flask.session['credentials'] = {
'token': credentials.token,
'refresh_token': credentials.refresh_token,
'token_uri': credentials.token_uri,
'client_id': credentials.client_id,
'client_secret': credentials.client_secret,
'scopes': credentials.scopes
}
data1=flask.redirect(flask.url_for('index'))
data1.headers['Access-Control-Allow-Origin'] = '*'
return data1
出现错误时,