Search code examples
angularjsgoogle-oauthinappbrowser

Required Parameter is missing grant_type Google oauth2.0 AngularJS and Cordova inappbrowser


I am using Cordova's inappbrowser and integrating Google oauth2.0. Once I get the authorization code I make a post request to get my token. NO MATTER what I try I always get a 400 error with "Required Parameter is missing grant_type". I'm encoding uri, I'm setting the right headers but to no avail... can anyone help?

                    $http({
                    method: 'POST',
                    url: 'https://accounts.google.com/o/oauth2/token',
                    params:{code:authorization_code[0],
                        client_id:options.client_id,
                        client_secret:options.client_secret,
                        redirect_uri:options.redirect_uri,
                        grant_type:'authorization_code'},
                    headers:{
                        'Content-Type':'application/x-www-form-urlencoded',
                    }
                }).success(function(data,status,headers,config){
                    deferred.resolve(data);
                }).error(function(data, status,headers,config){
                    console.log('data, status, headers,config',data,status,headers,config);
                    deferred.reject(response.responseJSON);
                });

and this is the output from the Chrome dev Console when I try to make the request

enter image description here

Request URL:https://accounts.google.com/o/oauth2/token?client_id=736406995874-oh7o4cmaju3jgprllln97nf0p3pc1f91.apps.googleusercontent.com&client_secret=ysgrIV6mJXxritfXnRcclV_U&code=4%2FnITDK731NhavPePthrVA1eX8LHFC.ojUX9K7DpBYaEnp6UAPFm0HWDS5njgI&grant_type=authorization_code&redirect_uri=http:%2F%2Flocalhost

Request Method:POST

Status Code:400 Bad Request

Request Headers

POST https://accounts.google.com/o/oauth2/token?client_id=xxx-oh7o4cmaju3jgprllln97nf0p3pc1f91.apps.googleusercontent.com&client_secret=xxx&code=4%2FnITDK731NhavPePthrVA1eX8LHFC.ojUX9K7DpBYaEnp6UAPFm0HWDS5njgI&grant_type=authorization_code&redirect_uri=http:%2F%2Flocalhost HTTP/1.1

Accept: application/json, text/plain, /

Origin: file://

testing: testing

User-Agent: Mozilla/5.0 (Linux; Android 4.4.2; SCH-I535 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Mobile Safari/537.36

Query String Parameters client_id=xxx-oh7o4cmaju3jgprllln97nf0p3pc1f91.apps.googleusercontent.com&client_secret=xxx&code=4%2FnITDK731NhavPePthrVA1eX8LHFC.ojUX9K7DpBYaEnp6UAPFm0HWDS5njgI&grant_type=authorization_code&redirect_uri=http:%2F%2Flocalhost

Response Headers HTTP/1.1 400 Bad Request Pragma: no-cache Date: Mon, 14 Jul 2014 06:35:22 GMT Content-Encoding: gzip X-Content-Type-Options: nosniff Server: GSE X-Frame-Options: SAMEORIGIN Content-Type: application/json Cache-Control: no-cache, no-store, max-age=0, must-revalidate Transfer-Encoding: chunked Alternate-Protocol: 443:quic X-XSS-Protection: 1; mode=block Expires: Fri, 01 Jan 1990 00:00:00 GMT


Solution

  • Wrong post request. The params property is used to set any additional request parameters to be appended to the URL query string. The params property is a JavaScript object with one property per request parameter to add.

    Here for your reference.