【问题标题】:google oauth2 get token javascript post requestgoogle oauth2 获取令牌 javascript 发布请求
【发布时间】:2015-07-21 14:37:58
【问题描述】:

我看到了一些关于此的问题和答案,但不明白该怎么做。 我收到此错误: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);
});

【问题讨论】:

    标签: javascript oauth oauth-2.0 google-api google-oauth


    【解决方案1】:

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

    获取 OAuth 密钥:

    • 访问谷歌云控制台
    • 点击创建项目按钮
    • 输入项目名称,然后点击创建
    • 然后从侧边栏中选择 APIs & auth 并单击 Credentials 选项卡

    注意:确保您已打开所需的 API。

    重要的部分是:Authorized Javascript origins: http://localhost:63342,您需要对您的网站域进行自动访问以访问 API。

    端点不好用https://www.googleapis.com/oauth2/v3/tokenGoogle 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)}
    });
    

    【讨论】:

    • 添加了您告诉我的域。还添加了你说的API。错误依然存在
    • 有时您必须等待 4/5 分钟。
    • 那么问题出在 Ajax 调用中
    猜你喜欢
    • 2013-08-03
    • 1970-01-01
    • 2016-10-13
    • 2015-04-09
    • 2016-06-22
    • 1970-01-01
    • 1970-01-01
    • 2013-12-27
    • 2016-06-26
    相关资源
    最近更新 更多