Help needed: Gmail contact import script for Drupal not working as expected

Hey everyone! I’m having trouble with a Drupal module for importing Gmail contacts. It works fine for the first 25 contacts but breaks when I try to get more.

Here’s what’s happening:

When I change the URL from:
http://www.google.com/m8/feeds/contacts/default/thin
to:
http://www.google.com/m8/feeds/contacts/default/thin?max-results=1000

I get this error:

Fatal error: Call to a member function getAttribute() on a non-object in path/to/site/sites/all/modules/dcl_importer/scripts/importGData.class.php on line 97

The error is on this line: $email = $mnode->getAttribute('address');

I’ve included the full script in a code block below. Can anyone help me figure out why it’s not working when I try to get more than 25 contacts? I really want to pull in a user’s entire contact list.

class GDataMailer {
  // ... [rest of the class code] ...
}

Any ideas on how to fix this? It would be a huge help for the Drupal community. Thanks!

I had a similar issue with Gmail contact imports in Drupal. In my experience, trying to retrieve all contacts at once can lead to problems because the API handles pagination differently when more than a few contacts are requested. Instead of changing the URL to force a large number, it worked better to start with the default approach, retrieve the initial batch, and then check if a ‘next’ link is available in the response. If you find such a link, use it to fetch the next batch of contacts until you have everything. This method not only avoids the error you saw but also helps manage timeouts and memory issues. Also, verify that you are using the latest version of the Google API client library since older versions might not handle pagination properly.

Having dealt with Gmail API integrations before, I can suggest a few things to try. First, ensure you’re using the latest version of the Google API client library for PHP. Older versions might not handle larger result sets correctly.

Consider implementing pagination in your code. Instead of requesting 1000 contacts at once, fetch them in smaller batches (say, 100 at a time) and use the ‘next’ token provided in the API response to get subsequent pages. This approach is more robust and less likely to hit API limits or timeout issues.

Also, double-check your OAuth scope. Make sure you have the correct permissions to access the user’s contacts. Sometimes, API errors can occur due to insufficient access rights.

Lastly, implement proper error handling and logging in your script. This will help you identify exactly where and why the script is failing when dealing with larger datasets.

yo harry, had similar probs. try breaking it up into chunks mate. instead of grabbing 1000 at once, fetch like 50-100 at a time. then use the ‘next’ token to grab more. helps avoid timeouts n stuff. also double check ur oauth setup, sometimes permissions can be wonky. good luck bruv!