您如何使用查询参数来限制Google Contacts API中检索到的联系人 [英] How do you use query parameters to limit retrieved contacts in the Google Contacts API

查看:71
本文介绍了您如何使用查询参数来限制Google Contacts API中检索到的联系人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JavaScript测试Google Contacts API.我知道V1支持JavaScript,但V2和V3并未列出.但是,我已经找到了一种通过AJAX请求检索联系人的方法,因此现在获取联系人并不是真正的问题.但是,我希望能够指定要查找的联系人的搜索条件,从而不必翻页和搜索所有用户联系人.

I'm trying to test out the Google Contacts API with JavaScript. I know that V1 had support for JavaScript but V2 and V3 don't list it. However, I have been able to find a way to retrieve contacts through an AJAX request so getting the contacts isn't really the problem right now. However, I'd like to be able to specify the search criteria of the contact that I'm looking for so that I don't have to page and search through all of the users contacts.

现在,我的代码如下:

function getContact(domTarget) {
   // Get permission and a token
   auth(function(token) {

      // Create an AJAX request to get the contacts
      var ajaxContactRequest = new XMLHttpRequest(),
              url = "";

      // What to do when we get our contacts
      ajaxContactRequest.onreadystatechange = function() {
         if (ajaxContactRequest.readyState === 4 && ajaxContactRequest.status === 200) {

            // Parse our contact data as a JSON object
            var response = JSON.parse(ajaxContactRequest.responseText),
                    contactArray = response.feed.entry, i, contactDiv;

            // Print out the contacts
            if (domTarget) {
               for (i = 0; i < contactArray.length; i++) {
                  if (contactArray[i].title && contactArray[i].title.$t) {
                     contactDiv = document.createElement("div");
                     domTarget.appendChild(contactDiv);

                     // Print out the contact's name
                     contactDiv.innerHTML = contactArray[i].title.$t;
                  }
               }
            }
         }
      };

      // Construct our URL
      url += "https://www.google.com/m8/feeds/contacts/default/full/";
      // Add the access token
      url += "?access_token=" + token.access_token;
      // Get JSON format back
      url += "&alt=json";
      // Limit to 10 results
      url += "&max-results=10";

      // Open the request
      ajaxContactRequest.open("GET", url, false);

      // Send it away
      ajaxContactRequest.send();
   });
}

我知道对查询有某种支持,因为此处列出了:使用查询参数检索联系人提到您可以使用在此处找到的参数:

I know that there is some sort of support for queries because the listing here: Retrieving Contacts Using Query Parameters mentions that you can use the parameters found here: Contacts Query Parameters Reference which lists q=term1 term2 term 3 and `q="term1 term2 term3" are the way to do "Fulltext query on contacts data fields".

我尝试了几种不同的参数组合,例如名字和姓氏以及日期和电子邮件.但是,它们都不会对结果产生任何影响.我想知道的另一件事是,这是否真的很重要.我仅基于名称限制查询,以尝试减少响应的大小.但是,如果更好的做法是只获取整个联系人集,然后收集所需的信息,我想我可以代替它.

I have tried several different mixes of parameters such as first and last name as well as dates and emails. However, none of them have any impact on the results. Another thing that I am wondering is if this really matters. I'm limiting my query based on name only to try and reduce the size of the response. However, if the better practice is to just grab the whole contact set and then glean the bits of info that you need, I guess I can do that instead.

我认为如果联系人数量大于max-results大小,则您必须执行多个请求.

I figure you have to do multiple requests if the number of contacts is larger than the max-results size.

推荐答案

您需要切换到Google联系人API的版本3.

You need to switch to version 3 of Google Contacts API.

v1和v2不支持搜索词"(q)参数,因此您只能使用最后更新"和排序依据"之类的查询参数.

v1 and v2 don't have support for the "search term" (q) parameter, so you're limited to query parameters like "last updated" and "order by" and such.

所以.首先,它们暗示所有标头都需要"GData-Version:3.0"参数. https://developers.google.com/google-apps/contacts/v3 /#specifying_a_version

So. First off they hint that all of your headers need the "GData-Version: 3.0" parameter. https://developers.google.com/google-apps/contacts/v3/#specifying_a_version

如果您使用的是jQuery,那将非常简单.例如:设置请求标头jQuery Ajax

If you're using jQuery, that's fairly straightforward. Here's an example: Set Request Header jQuery Ajax

但是在您已经获得的代码中,您可以将其添加到所有请求的URL中.

But in the code you've got already, you can just add it to the URL for all requests.

url += "https://www.google.com/m8/feeds/contacts/default/full/?v=3";

(同样在jQuery中,如果修改标头很可怕,也很容易将其添加为参数)

(again in jQuery, it's easy to add it as a param too if modifying the header is scary)

踢球者花了我数周的时间才弄清楚,这是因为作用域"本身是错误的.

The kicker, and it took me weeks to figure this out, is that the "scope" itself is wrong.

在您的HTML代码中,我确定您已使用...授权了您的应用程序.

In your HTML code I'm sure you've authorized your app using...

scope="https://www.google.com/m8/feeds"

...如文档中所述.但是,当他们说所有请求都需要标头"时,它们实际上是指所有请求.而且,如果您使用的是Google预先屏蔽的HTML示例来授权您的应用程序,则您根本无权访问允许您修改标头的代码.因此,您需要使用...

... as it says to do in the documentation. But when they say "all requests need the header" they really mean ALL requests. And if you're using Google's pre-canned HTML examples to authorize your application, then you simply don't have access to the code which would let you modify the headers. So instead you need to use...

scope="https://www.google.com/m8/feeds?v=3"

这是因为v2 API和v3 API共享一个范围URL,只有当您并排阅读v2.0和v3.0文档时,您才真正注意到该URL.

This is because the v2 API and v3 API share a scope URL, which you really only notice if you're reading the v2.0 and the v3.0 documentation side by side.

无论如何,我希望能有所帮助.

Anyway, I hope that helps.

基隆

这篇关于您如何使用查询参数来限制Google Contacts API中检索到的联系人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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