【问题标题】:Access all gmail contacts using access_token in asp.net在 asp.net 中使用 access_token 访问所有 gmail 联系人
【发布时间】:2012-10-18 10:31:15
【问题描述】:

我正在尝试在 asp.net 网络应用程序中访问 gmail 联系人。

我仍然可以从 google 获取 access_token,但是当我将此 access_token 发送到 google 联系人 api 时,它给了我错误。

下面是我从我的应用程序重定向的 URL,用户通过提供他的电子邮件和密码进行身份验证。

https://accounts.google.com/o/oauth2/auth?scope=https://www.google.com/m8/feeds&redirect_uri=http://localhost:3223/WebSite1/Default.aspx&response_type=token&client_id=881595232473.apps.googleusercontent.com

在此用户使用访问令牌返回我的 Web 应用程序之后。

这里我使用了两种不同的方法来获取所有联系人:

方法 1 - Web 请求

     HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create(url1);

    HttpWebResponse response1 = (HttpWebResponse)request1.GetResponse();

    System.IO.Stream ReceiveStream1 = response1.GetResponseStream();
    StreamReader readStream1 = new StreamReader(ReceiveStream1);
    string result = readStream1.ReadToEnd();

它工作正常,并给了我 XML 的结果。但问题是它只提供前 25 个联系人,而我总共有 246 个联系人。

方法 2 - Google Contact API

           RequestSettings rs = new RequestSettings("aman contact", Request.QueryString["access_token"].ToString());

    rs.AutoPaging = true;
    ContactsRequest cr = new ContactsRequest(rs);
    PrintAllContacts(cr);
    Feed<Contact> f = cr.GetContacts();

这里给了我以下错误:

Execution of request failed: http://www.google.com/m8/feeds/contacts/default/full

在此之后它向我显示一个带有以下错误的黄页:

The remote server returned an error: (401) Unauthorized.

【问题讨论】:

    标签: asp.net google-apps google-contacts-api


    【解决方案1】:

    我只能评论您关于方法 1 - Web 请求的问题,因为我没有使用 .Net 与 Google Contacts API 集成。

    我建议您尝试为max-query 传入一个值(默认为25)。根据我的经验,从 api 中提取大约 500 个联系人需要不到一秒钟的时间,因此您应该能够一次安全地查询您的特定通讯录。但是,您应该根据应用程序的要求调整此值。例如,如果您需要您的应用程序响应速度非常快,您可能希望使该值更小,以便您可以开始更快地填充联系人。如果您在后台执行此工作,则等待 5 秒等待 5000 个联系人可能是可以接受的。

    由于人们的通讯簿大小差异很大,您需要能够使用start-index 参数多次查询API。第一次查询后,看看这里返回的值:

      <openSearch:totalResults>1</openSearch:totalResults>
      <openSearch:startIndex>1</openSearch:startIndex>
      <openSearch:itemsPerPage>25</openSearch:itemsPerPage>
    

    total-results 将让您计算需要使用不同的start-index 查询多少次才能获取所有数据。

    Google Contacts API (v3) 描述了这些parameters。作为警告,start-index 是联系人数组中基于 1 的索引,而不是页面索引,因此您必须进行数学运算。例如,您可以请求max-query=25&amp;start-index=26 访问联系人的第 2 页。

    【讨论】:

    • 是的 Graeme,我用过这个参数。它可以工作并给我所有联系人,但我试图使用 Google Contact API,因为它确实会消除解析方法中返回的大 XML 的开销 - 1. 顺便说一句,非常感谢..
    猜你喜欢
    • 2014-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多