2

I can't find the solution for my problem. I'm trying to get google access token over OAuth 2.0 but I can't make my post method work. This is my code:

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"});
        }
    }

Using postman I've got access_token so the url and the data is correct. I've tried this and jsonp and many other answers but I always get the same result:

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.

What am I doing wrong?

Community
  • 1
  • 1
Mario Rudman
  • 1,477
  • 2
  • 10
  • 15

1 Answers1

3

You're not going to be able to get around the security if you're using AJAX it's always going to block you with this method. I see 2 options.

  1. Change your AJAX request to be a browser redirect.
  2. Create a proxy server on your server that will forward the request on to google and then return the response.

This looks like a duplicate of Google oauth 400 response: No 'Access-Control-Allow-Origin' header is present on the requested resource

Community
  • 1
  • 1
Nico Westerdale
  • 2,154
  • 1
  • 20
  • 30