不能使用angularjs从谷歌获得访问令牌 [英] cant get access token from google using angularjs

查看:313
本文介绍了不能使用angularjs从谷歌获得访问令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在如下因素本指南并试图获得accses从谷歌API令牌。我alredy得到一个auth code。所以我使用angularjs发送HTT prequest到谷歌服务器。问题是,我总是得到错误的请求的答复。这是我的code:

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');

        });

我用<一个href=\"https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm\"相对=nofollow>邮递员chorme插件来测试我的参数,可以和它的工作的邮递员。
那里的错误是因为此请求的AUTH code已经过期,但ST工作!

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!

没有任何人有一个sulotion这个问题吗?
非常感谢!

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

推荐答案

我想,如果你是来比较的角度所产生的结果的请求和一个接邮递员送到你会发现这里的区别。 (看看小提琴手来实现这一点)。

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).

对象映射您传递的'参数'被映射到一个查询字符串参数,但是谷歌期望在请求主体此数据。要做到这一点,然后其设置为数据参数。但是在默认情况下角将发送此为JSON,所以我们需要它发布之前转换这一点。这就是transformRequest功能的目的。 (请参见此链接为解说)

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)

请参见下面改写code。

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; 
       }
    })

这篇关于不能使用angularjs从谷歌获得访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆