-1

Does the contacts API still work for anyone? A while back I started getting 403 errors from the API. I run my backup script once a week. Every user that I can't back up, I retry every hour until the next week. Over the span of the week I will end up with a couple of contacts entries, but not a significant amount. This leads me to believe that my code still works since I get some contacts. Anyone have any insight?

Note: I also use the same code/framework to backup Google Drive and Google Calendars for my organization and have not had any issues.

function retrieveAllUserContacts($user)
{
    $nextLink = "https://www.google.com/m8/feeds/contacts/$user/full";
    $params = array('xoauth_requestor_id' => $user);
    while($nextLink != '')
    {
        $header = array('GData-Version: 3.0');
        $result = sendOAuthRequest('GET', $nextLink, $params, $header);
        $params = array('xoauth_requestor_id' => $user);
        $nextLink = '';
        libxml_use_internal_errors(true);
        $xmlObj = simplexml_load_string($result);
        if($xmlObj === false)
        {
            echo "adding $user to retry list. Result : " . print_r($result, true) . "\n";
            addUserToRetryList($user);
            exit(1);
        }
        foreach($xmlObj->link as $link)
        {
            if($link['rel'] == 'next')
            {
                $nextLink = $link['href'];
            }
            if($nextLink != '')
            {
                $urlSplit = explode('?', $nextLink);
                $nextLink = $urlSplit[0];
                $urlParams = explode('&', $urlSplit[1]);
                foreach($urlParams as $urlParam)
                {
                    $urlParamSplit = explode('=', $urlParam);
                    $params[$urlParamSplit[0]] = $urlParamSplit[1];
                }
                break;
            }
        }
        foreach($xmlObj->entry as $entry)
        {//get contacts
 ...
}

function sendOAuthRequest($httpMethod, $url, $parameters, $header=array())
{
    global $CONSUMER_KEY;
    global $CONSUMER_SECRET;
    $consumer = new OAuthConsumer($CONSUMER_KEY, $CONSUMER_SECRET, NULL);
    $request = OAuthRequest::from_consumer_and_token($consumer, NULL, $httpMethod, $url, $parameters);
    // Sign the constructed OAuth request using HMAC-SHA1  
    $request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $consumer, NULL);  
    // Make signed OAuth request to the Contacts API server
    if(count($parameters))
    {
        if(strpos($url, '?') === false)
        {
            $url .= '?' . implode_assoc('=', '&', $parameters);
        }
        else
        {
            $url .= '&' . implode_assoc('=', '&', $parameters);
        }
    }
    $header[] = $request->to_header();
    return send_request($request->get_normalized_http_method(), $url, $header);
}

Here is the OAuth Class code : http://pastebin.com/hH4SM9nn I can say that it works for Google Drive and Google Calendar APIs and also that this code worked for over a year without issue.

user1538393
  • 33
  • 1
  • 7

1 Answers1

0

A number of problems could be causing this. Since your question isn't specific and gives no code sample, here are some of those potential problems:

  • You have not moved to HTTPS as opposed to HTTP
  • your GET request specifies a "default" user, where the authority is not always given
  • You are trying to get too many contacts

There are also issues with authorisation and permissions as of late, one "fix" as such is documented in this StackOverflow answer. It concerns adding not only a google account, but the gmail address associated with that account to the analytics service, via this link.

Community
  • 1
  • 1
Wolfish
  • 960
  • 2
  • 8
  • 34
  • 1
    I am using https. I would imagine that it would never work if I wasn't. I will get the code together now. – user1538393 Aug 06 '14 at 13:50
  • 1
    @user1538393 Has it resolved your issues? There is potential for it to work occasionally even without https, given that not all countries allow external https traffic. That, and glitches in the matrix. – Wolfish Aug 06 '14 at 14:04
  • 1
    No since I've had it using https since I wrote it over a year ago and the issues started arising a month or two ago. I've added all the code. Thanks for looking at it. – user1538393 Aug 06 '14 at 14:06
  • 1
    Also, as you can see my code doesn't not violate the bullet points you had posted. 1) It uses https 2) It uses a service account to impersonate all the users in my domain 3) I am getting the contacts at the rate that Google provides without increasing the threshold. I don't use any constraints other than the username – user1538393 Aug 06 '14 at 14:09
  • 1
    @user1538393 Oh, beg pardon. I thought you meant you had modified your code to use HTTPS. – Wolfish Aug 06 '14 at 14:34
  • 1
    No worries, I appreciate anyone looking at this. haha – user1538393 Aug 06 '14 at 14:37
  • 1
    @user1538393 Added some info, take a look – Wolfish Aug 06 '14 at 15:22