谷歌oauth2获取令牌javascript发布请求

谷歌oauth2获取令牌javascript发布请求

本文介绍了谷歌oauth2获取令牌javascript发布请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了一些关于这个问题的答案,但不知道该怎么做。
我得到这个错误: XMLHttpRequest无法加载https://accounts.google.com/o/oauth2/token。请求的资源上没有Access-Control-Allow-Origin标题。原因'http:// localhost:63342'因此不被允许访问。这个响应的HTTP状态码是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'
};
$ b $ .post(url,{
form:payload
},function(error,response,body){
console.log(body);
});


解决方案

您是否注册了您的应用程序?

注意:确保已打开所需的API。
$ b 重要的部分是:授权的Javascript起源: ,您需要自行确定您的网站域以访问API。

b
$ b

端点不好使用 https://www.googleapis.com/oauth2/v3/token

  $。ajax({
url:https://www.googleapis.com/ oauth2 / v3 / token,
data:{
code:,
client_id:,
client_secret:,
redirect_uri:,
grant_type:authorization_code
},
方法:POST,
success:function(e){console.log(e)}
});


I saw some questions and answers about this but couldn't understand what to do.I get this error: 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.As I saw from previous posts, this is because I can't make an HTTP POST request to another server. I saw some things about using jsonp but couldn't understand how..This is the function I use to send the request:

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);
});
解决方案

Have you registered your app ?

Note: Make sure you have turned on the APIs you need.

The important part is : Authorized Javascript origins: http://localhost:63342, you need to autorize your website domain to access the API.

The endpoint is bad use 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)}
});

这篇关于谷歌oauth2获取令牌javascript发布请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 13:08