0

I'm trying to access gmail contacts in asp.net web application.

Still I'm able to get access_token from google, but when I send this acccess_token to google contact api, its giving me error.

Below is the url where I redirect from my application and user authenticates by giving his email and password.

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

After this user came back to my web application with access token.

Here I've used two diferent methods to get all contacts:

Method 1 - Web Request:

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

Its working fine and gives me result in XML. But problem is that its giving only first 25 contacs while i've total 246 contacts.

Method 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();

Here its giving me following error:

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

after this it shows me a yellow page with following error:

The remote server returned an error: (401) Unauthorized.
Kara
  • 6,115
  • 16
  • 50
  • 57

1 Answers1

1

I can only comment on your question about Method 1 - Web Request, since I haven't used .Net to integrate with Google Contacts API.

I suggest that you try passing in a value for max-query (it is 25 by default). In my experience pulling about 500 contacts from the api takes less than one second, so you should be able to safely query you particular address book in one shot. However, you should tune this value to your application's requirements. For example, if you need your application to be very responsive, you may want to make this value smaller, so you can start to populate the contacts faster. If you are doing this work in the background, then waiting 5 seconds for 5000 contacts may be acceptable.

Because people had widely varying sizes of address book, you need to be able to query the API multiple times using the start-index parameter. After the first query, take a look at the values returned here:

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

total-results will let you compute how many times you'll need to query with a different start-index to get all the data.

The Google Contacts API (v3) describes these parameters. As a word of warning, start-index is a 1 based index into the array of contacts, not a page index, so you have to do the math. For example, you would request max-query=25&start-index=26 to access page 2 of the contacts.

Graeme
  • 970
  • 8
  • 16
  • yes Graeme, I've used this parameter. Its working and giving me all contacts but i was trying to use Google Contact API because it'll really remove overhead for Parsing a Big XML returned in Method - 1. by the way, thanks a lot.. –  Oct 22 '12 at 06:04