【问题标题】:Required Parameter is missing grant_type Google oauth2.0 AngularJS and Cordova inappbrowser必需的参数缺少 grant_type Google oauth2.0 AngularJS 和 Cordova inappbrowser
【发布时间】:2014-09-04 00:37:39
【问题描述】:

我正在使用 Cordova 的 inappbrowser 并集成 Google oauth2.0。获得授权码后,我会发出一个发布请求以获取我的令牌。不管我尝试什么,我总是得到一个 400 错误“必需参数缺少 grant_type”。我正在编码 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);
                });

这是我尝试发出请求时 Chrome 开发控制台的输出

请求网址: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

请求方法:POST

状态码:400 错误请求

请求标头

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

接受: application/json, text/plain, /

来源:file://

测试:测试

用户代理: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

查询字符串参数 client_id=xxx-oh7o4cmaju3jgprllln97nf0p3pc1f91.apps.googleusercontent.com&client_secret=xxx&code=4%2FnITDK731NhavPePthrVA1eX8LHFC.ojUX9K7DpBYaEnp6UAPFm0HWDS5njgI&grant_type=authorization_code&redirect_uri=http:%2F%2F

响应标头 HTTP/1.1 400 错误请求 Pragma:无缓存 日期:2014 年 7 月 14 日星期一 06:35:22 GMT 内容编码:gzip X-Content-Type-Options: nosniff 服务器:GSE X 框架选项:SAMEORIGIN 内容类型:应用程序/json 缓存控制:无缓存,无存储,最大年龄=0,必须重新验证 传输编码:分块 替代协议:443:quic X-XSS-防护:1;模式=块 过期:格林威治标准时间 1990 年 1 月 1 日星期五 00:00:00

【问题讨论】:

    标签: angularjs google-oauth inappbrowser


    【解决方案1】:

    发帖请求错误。 params 属性用于设置附加到 URL 查询字符串的任何其他请求参数。 params 属性是一个 JavaScript 对象,每个请求参数添加一个属性。

    Here for your reference.

    【讨论】:

      【解决方案2】:

      您只需要以序列化的形式发送数据/参数(对于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);
                  });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-29
        • 2012-06-27
        • 2019-12-11
        • 2012-01-02
        • 1970-01-01
        • 2019-09-14
        相关资源
        最近更新 更多