node.js - Error when trying to use google oAuth API with hapi/bell js
问题描述
I am trying to use hapi/bell and google API oAuth but i am having this 2 errors in the console:
Access to XMLHttpRequest at 'https://accounts.google.com/o/oauth2/v2/auth?client_id=914091897182-m38hh6r2t4nhur4fgs1dn7v790l0bvrr.apps.googleusercontent.com%A&response_type=code&redirect_uri=http%3A%2F%2FLAPTOP-CEDRIC%3A3000%2Fbell%2Fdoor&state=Bj4hjqaQ0qdYuVich4ESje&scope=profile%20email' (redirected from 'http://localhost:3000/bell/door') from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Then:
jquery.min.js:2 GET https://accounts.google.com/o/oauth2/v2/auth?client_id=914091897182-m38hh6r2t4nhur4fgs1dn7v790l0bvrr.apps.googleusercontent.com%A&response_type=code&redirect_uri=http%3A%2F%2FLAPTOP-CEDRIC%3A3000%2Fbell%2Fdoor&state=Bj4hjqaQ0qdYuVich4ESje&scope=profile%20email net::ERR_FAILED
The code follows the example from bell: https://hapi.dev/module/bell/examples/#google
and in the front end i use in the header (with pug):
script.
window.onLoadCallback = function start() {
gapi.load('auth2', function() {
auth2 = gapi.auth2.init({
client_id: 'MYCLIENTID',
// Scopes to request in addition to 'profile' and 'email'
//scope: 'additional_scope'
});
});
}
then in the body:
script.
function signInCallback(authResult) {
if (authResult['code']) {
// Hide the sign-in button now that the user is authorized, for example:
$('#signinButton').attr('style', 'display: none');
// Send the code to the server
$.ajax({
type: 'POST',
url: 'http://localhost:3000/bell/door',
// Always include an `X-Requested-With` header in every AJAX request,
// to protect against CSRF attacks.
headers: {
'X-Requested-With': 'XMLHttpRequest'
},
contentType: 'application/octet-stream; charset=utf-8',
success: function (result) {
// Handle or verify the server response.
},
processData: false,
data: authResult['code']
});
} else {
// There was an error.
}
}
解决方案
推荐阅读
- angularjs - 在AngularJS中重置选择标签
- spring - S3:将文件放置在多个目标存储桶中的出站适配器
- angular - 使用多张纸创建 angular5-csv
- linux - Linux脚本抛出错误,虽然它看起来完全正常
- java - 在每个方法调用中使用 newInstance() 创建 JAXBContext 是否正确?
- java - 检查方法是否返回相应类的实例
- xml - XML 正则表达式
- google-bigquery - 有没有办法在 BigQuery 中检查查询是否命中缓存结果?
- java - 度量单位 API (JSR-363) - 浮点错误
- jupyter-notebook - 从 jupyter notebook 转换为 tex 时的多行方程