Tomer Pearl
Tomer Pearl

Reputation: 107

cant get access token from google using angularjs

i am folowing this guide and trying to get a accses token from google api. i alredy got an auth code. so i'm using angularjs to send httprequest to google server. the problem is that i always get bad request answer. this is my code:

        $http({
            method: 'POST',
            url: 'https://accounts.google.com/o/oauth2/token',
            headers: {'Content-Type': 'application/x-www-form-urlencoded'},
            params : {
                code           : '4/heMv6ozWwCxS5RyTzCgThAgxvRyk.oske-bNEGOUTOl05ti8ZT3YnwwH8iQI',
                client_id      : GoogleAppInfo.clientId,
                 redirect_uri  : GoogleAppInfo.redirect_uri,
                client_secret  : GoogleAppInfo.client_secret,
                grant_type     : 'authorization_code'
            },
            data :'Demo' //just data to apply the heaser

        }).
        success(function(data, status, headers, config) {
            alert('secsses');
        }).
        error(function(data, status, headers, config) {
            alert('error');

        });

i have used PostMan chorme addon to test my params, and it's working on postman. The error down there is because the authcode for this request has expired, But it st working! enter image description here

does anybody have a sulotion to this problem? thanks a lot!!

Upvotes: 2

Views: 1383

Answers (1)

David Beech
David Beech

Reputation: 1108

I think if you were to compare the resulting request generated by angular and the one sent by postman you would spot the difference here. (check out fiddler to achieve this).

The object map your passing as the 'params' is mapped to a query string parameters however google is expecting this data in the request body. To do this then set this as the 'data' parameter. However by default angular will send this as json so we need to transform this before it is posted. that's the purpose of the transformRequest function. (see this link for explanation)

See rewritten code below.

$http({
    method: 'POST',
    url: 'https://accounts.google.com/o/oauth2/token',
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    data: {
            code           : '4/heMv6ozWwCxS5RyTzCgThAgxvRyk.oske bNEGOUTOl05ti8ZT3YnwwH8iQI',
            client_id      : GoogleAppInfo.clientId,
            redirect_uri  : GoogleAppInfo.redirect_uri,
            client_secret  : GoogleAppInfo.client_secret,
            grant_type     : 'authorization_code'
        }
     transformRequest: function(data) {
         var buffer = [];
         for ( var name in data ) {
             if ( ! data.hasOwnProperty( name ) ) {
                 continue;
             }
             var value = data[ name ];
             buffer.push(
                        encodeURIComponent( name ) +
                        "=" +
                        encodeURIComponent( ( value == null ) ? "" : value )
             );
         }
          var source = buffer
              .join( "&" )
              .replace( /%20/g, "+" );
          return source; 
       }
    })

Upvotes: 1

Related Questions