我看到了一些有关此问题和答案,但不知道该怎么办。我收到此错误:XMLHttpRequest cannot load https://accounts.google.com/o/oauth2/token. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access. The response had HTTP status code 400.
从以前的帖子中可以看到,这是因为我无法向另一台服务器发出HTTP POST请求。我看到了一些有关使用jsonp的信息,但无法理解如何使用。.这是我用来发送请求的函数:
var url = 'https://accounts.google.com/o/oauth2/token';
var payload = {
grant_type: 'authorization_code',
code: authResult['code'],
client_id: clientID,
client_secret: clientSecret,
redirect_uri: '',
dataType: 'jsonp'
};
$.post(url, {
form: payload
}, function(error, response, body) {
console.log(body);
});
您已经注册了应用程序吗?
获取OAuth密钥:
- 访问Google Cloud Console
- 单击创建项目按钮
- 输入项目名称,然后单击创建。
- 然后从侧边栏中选择API和身份验证,然后单击“凭据”选项卡
- 单击创建新的客户ID按钮
- 应用程序类型:Web应用程序
- 授权的JavaScript来源:http:// localhost:63342
- 授权的重定向URI:http:// localhost:63342 / ...
注意:确保已打开所需的API。
重要的部分是:授权的Javascript起源:http:// localhost:63342,您需要对您的网站域进行授权以访问API。
端点使用https://www.googleapis.com/oauth2/v3/token
Google Doc是错误的:
$.ajax({
url: "https://www.googleapis.com/oauth2/v3/token",
data: {
code :"",
client_id : "",
client_secret : "",
redirect_uri : "",
grant_type : "authorization_code"
},
method: "POST",
success: function(e){console.log(e)}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句