XMLHttpRequest无法加载https://accounts.google.com/o/oauth2/token,Angularjs

时间:2014-08-14 01:06:58

标签: javascript angularjs oauth

我无法找到解决问题的方法。我试图通过OAuth 2.0获取谷歌访问令牌,但我不能让我的帖子方法工作。这是我的代码:

function oauthCallback(url) {
        var code,
            obj, request;
        if (url.indexOf("code=") > 0) {
            code = url.substr(url.indexOf('=') + 1);
            request = $http({
                method: "POST",
                url: GOOGLE_TOKEN_URL,
                headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                data: 'code=' + code + '&client_id=' + googleClientID + '&client_secret=' + googleClientSecret + '&redirect_uri=' + redirectURI + '&grant_type=authorization_code'
            });
            request.success( function(data) {
                tokenStore['googleToken'] = data.access_token;
            });
            request.error( function(data, status, headers, config) {
                alert('failed!')
            });
            deferredLogin.resolve();
        } else if (url.indexOf("error=") > 0) {;
            deferredLogin.reject(obj);
        } else {
            deferredLogin.reject({error: 'error occured', error_description: 'Unknown', error_reason: "Unknown"});
        }
    }

使用postman我有access_token所以url和数据是正确的。我已经尝试了this和jsonp以及许多其他答案,但我总是得到相同的结果:

  

XMLHttpRequest无法加载https://accounts.google.com/o/oauth2/token。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许原始'http://localhost:63342'访问。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

如果你使用AJAX,你将无法绕过安检,它总是会阻止你使用这种方法。 我看到了2个选项。

  1. 将您的AJAX请求更改为浏览器重定向。
  2. 在您的服务器上创建一个代理服务器,将请求转发给Google,然后返回响应。
  3. 这看起来像是重复的 Google oauth 400 response: No 'Access-Control-Allow-Origin' header is present on the requested resource

相关问题