简体   繁体   English

必需参数缺少grant_type Google oauth2.0 AngularJS和Cordova inappbrowser

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

I am using Cordova's inappbrowser and integrating Google oauth2.0. 我正在使用Cordova的inappbrowser并集成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". 无论我尝试什么,我总是得到400错误“必需参数缺少grant_type”。 I'm encoding uri, I'm setting the right headers but to no avail... can anyone help? 我正在编码uri,我正在设置正确的标题,但无济于事......任何人都可以帮忙吗?

                    $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 这是我尝试发出请求时Chrome开发者控制台的输出

在此输入图像描述

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 请求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 请求方法:POST

Status Code:400 Bad Request 状态代码:400错误请求

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 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, / 接受: application / json,text / plain, /

Origin: file:// 来源: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 用户代理:Mozilla / 5.0(Linux; Android 4.4.2; SCH-I535 Build / KOT49H)AppleWebKit / 537.36(KHTML,类似Gecko)版本/ 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 查询字符串参数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; 响应标头 HTTP / 1.1 400错误请求Pragma:no-cache日期:星期一,2014年7月14日06:35:22 GMT内容编码:gzip X-Content-Type-Options:nosniff服务器: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 mode = block到期日:1990年1月1日星期五00:00:00 GMT

Wrong post request. 错误的帖子请求。 The params property is used to set any additional request parameters to be appended to the URL query string. params属性用于设置要附加到URL查询字符串的任何其他请求参数。 The params property is a JavaScript object with one property per request parameter to add. params属性是一个JavaScript对象,每个请求参数都有一个属性要添加。

Here for your reference. 这里供您参考。

You just need to send data/params in serialized (for angular use $httpParamSerializer ) 你只需要在序列化中发送数据/参数 (对于angular使用$httpParamSerializer

 $http({
                method: 'POST',
                url: 'https://accounts.google.com/o/oauth2/token',
                params:$httpParamSerializer({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);
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM