2

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!!

1
  • I used server code to get my token. I don't think you want to put your secrets on the client device. Commented Mar 6, 2016 at 0:20

1 Answer 1

1

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; 
       }
    })
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.