google oauth2 获取令牌 javascript post 请求
google oauth2 get token javascript post request
我看到了一些关于此的问题和答案,但不明白该怎么做。
我收到此错误: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);
});
你有没有注册你的应用程序?
Obtaining OAuth Keys :
- Visit Google Cloud Console
- Click CREATE PROJECT button
- Enter Project Name, then click CREATE
- Then select APIs & auth from the sidebar and click on Credentials tab
- Click CREATE NEW CLIENT ID button
- Application Type: Web Application
- Authorized Javascript origins: http://localhost:63342
- Authorized redirect 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)}
});
我看到了一些关于此的问题和答案,但不明白该怎么做。
我收到此错误: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);
});
你有没有注册你的应用程序?
Obtaining OAuth Keys :
- Visit Google Cloud Console
- Click CREATE PROJECT button
- Enter Project Name, then click CREATE
- Then select APIs & auth from the sidebar and click on Credentials tab
- Click CREATE NEW CLIENT ID button
- Application Type: Web Application
- Authorized Javascript origins: http://localhost:63342
- Authorized redirect 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)}
});