使用oauth 2.0检索谷歌联系人

时间:2012-08-06 10:20:49

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

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

  

https://www.google.com/m8/feeds/contacts/xyz@gmail.com/full?oauthtoken= [对象%20Object]安培;回调= 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));
                    }
});

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

我回答了这个问题:

// 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

答案 2 :(得分:0)

您可以尝试在标题中将您的令牌写为“授权:承载”+ 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,因此它是可选的。

希望这有帮助