【问题标题】:Retrieving google contacts using oauth 2.0使用 oauth 2.0 检索谷歌联系人
【发布时间】:2012-08-03 07:30:58
【问题描述】:

我在这里一直在努力获取google联系人,我得到了用户的个人信息,但无法获取用户的联系人。 收到以下错误: 401(需要授权)

https://www.google.com/m8/feeds/contacts/xyz@gmail.com/full?oauthtoken=[object%20Object]&callback=jQuery162xxxxxxxxxx_13xxxxxxxxxx2&_=13xxxxxx。

$.ajax({
                    url: "https://www.google.com/m8/feeds/contacts/default/full",
                    dataType: "jsonp",
                    headers: "GData-Version: 3.0",
                    data:{accessToken: authResult },
                    success: function (data) {
                        console.log("json"+JSON.stringify(data));
                    }
});

【问题讨论】:

  • data:{accessToken: authResult } 应该是 data: JSON.stringify({accessToken: authResult }, null, ' ') 以便您的身份验证令牌被正确编码并且您不会得到 [object%20Object]。

标签: oauth-2.0 google-contacts-api google-api-client


【解决方案1】:

在您的第一个代码块(带有 URL)中,您指定查询参数名称 oauthtoken。在第二个代码块中,您指定参数值accessToken。

这个参数值实际上应该是access_token。看这里: https://developers.google.com/accounts/docs/OAuth2UserAgent#callinganapi

【讨论】:

    【解决方案2】:

    我回答了这个:

    // OAuth configurations   
        var config = {
          'client_id': 'xxxxxx.apps.googleusercontent.com',
          'scope': 'https://www.google.com/m8/feeds/contacts/default/full'          
        };
    
    gapi.auth.authorize(config, function(data) {
          // login complete - now get token
          var token = gapi.auth.getToken();
          token.alt = 'json';
          // retrieve contacts
          jQuery.ajax({
            url: 'https://www.google.com/m8/feeds/contacts/default/full/?max-results=999999',
            dataType: 'jsonp',
            data: token,
            success: function(data) { successGmail(data); }
          });
        });
    

    那里:Modify HTTP Headers for a JSONP request

    【讨论】:

      【解决方案3】:

      您可以尝试在您的标头中将您的令牌写为“授权:承载”+ authResult,所以我认为您的代码在此示例中应如下所示:

      $.ajax({
             url: "https://www.google.com/m8/feeds/contacts/default/full",
             type: "GET",
             dataType: "jsonp",
             headers: {
                         "Authorization": "Bearer " + authResult,
                         "GData-Version": "3.0"
                      },
             success: function (data) {
                  console.log("json"+JSON.stringify(data));
             }
      });
      

      JQuery Docs 对 ajax headers 参数有如下说明:

      与请求一起发送的附加标头键/值对的映射[...]

      所以这就是我将您的原始字符串更改为地图的原因。类型默认是 GET,所以它是可选的。

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-03
        • 1970-01-01
        • 2012-11-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多