开发者

Google Contact API Error: Request via script load timed out. Possible causes: feed URL is incorrect; feed requires authentication

Hi I have an Error on Google Contact JavaScript API. The开发者_JAVA百科 code was working fine from last day. But its not working today. Don't know what went wrong. :(

Request via script load timed out. Possible causes: feed URL is incorrect; 
feed requires authentication.


<html>
<head>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">google.load("gdata", "1.x");</script>

<script type="text/javascript">

    google.setOnLoadCallback(initFunc);

    var contactsService;

    function setupContactsService() {
      contactsService = new google.gdata.contacts.ContactsService('GoogleInc-jsguide-1.0');
    }

    function logMeIn() {
      var scope = 'https://www.google.com/m8/feeds';
      var token = google.accounts.user.login(scope);
    }

    function initFunc() {
      //logMeOut();
      setupContactsService();
      logMeIn();
      getMyContacts();
    }

    function getMyContacts() {
          var contactsFeedUri = 'https://www.google.com/m8/feeds/contacts/default/full';

          var query = new google.gdata.contacts.ContactQuery(contactsFeedUri);

          // Set the maximum of the result set to be 5
          query.setMaxResults(10000);

          contactsService.getContactFeed(query, handleContactsFeed, handleError);
    }

    var handleContactsFeed = function(result) {
    var entries = result.feed.entry;
      for (var i = 0; i < entries.length; i++) {
        var contactEntry = entries[i];
        var emailAddresses = contactEntry.getEmailAddresses();

        for (var j = 0; j < emailAddresses.length; j++) {
          var emailAddress = emailAddresses[j].getAddress();
        }    
      }
    }

    function handleError(e) {
          alert("There was an error!" + (e.cause ? e.cause.statusText : e.message));
          //alert(e.cause ? e.cause.statusText : e.message);
    }

    function logMeOut() {
        google.accounts.user.logout();
    }

    </script>
</head>
<body>
<IMG SRC="image.jpg"/> <!-- // Image for authentication -->
</body>
</html>


The following code works for me. Tested in Chrome, Safari, and FireFox. Summary of changes:

  • Check if logged in already, if so, don't attempt to log in again.
  • Removed google.setOnLoadCallback(initFunc); and replaced with a button click event. initFunc should never be called on page load.
  • IMPORTANT: If there is not an image on the page that is loaded from the same domain as your page, this will not work (per google).

Here's the code:

<html>
<head>
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">
        google.load("gdata", "1.x");
    </script>
    <script type="text/javascript">
        var contactsService;
        var scope = 'https://www.google.com/m8/feeds';

        function setupContactsService()
        {
            contactsService = new google.gdata.contacts.ContactsService('GoogleInc-jsguide-1.0');
        }

        function logMeIn()
        {
            var token = google.accounts.user.login(scope);
        }

        function initFunc()
        {
            setupContactsService();

            if (google.accounts.user.checkLogin(scope))
            {
                getMyContacts();
            }
            else
            {
                logMeIn();
            }
        }

        function getMyContacts()
        {
            var contactsFeedUri = 'https://www.google.com/m8/feeds/contacts/default/full';
            var query = new google.gdata.contacts.ContactQuery(contactsFeedUri);

            // Set the maximum of the result set to be 5
            query.setMaxResults(1);

            contactsService.getContactFeed(query, handleContactsFeed, handleError);
        }

        var handleContactsFeed = function (result)
        {
            var entries = result.feed.entry;
            for (var i = 0; i < entries.length; i++)
            {
                var contactEntry = entries[i];
                var emailAddresses = contactEntry.getEmailAddresses();

                for (var j = 0; j < emailAddresses.length; j++)
                {
                    var emailAddress = emailAddresses[j].getAddress();
                    alert(emailAddress);
                }
            }
        }

        function handleError(e)
        {
            alert("There was an error!" + (e.cause ? e.cause.statusText : e.message));
        }

        function logMeOut()
        {
            google.accounts.user.logout();
        }

    </script>
</head>
<body>
    <input type="button" onclick="initFunc();" value="Test" />
    <img src="image.jpg" />
    <!-- // Image for authentication -->

    <script type="text/javascript">
        if (google.accounts.user.checkLogin(scope))
        {
            setupContactsService();
            getMyContacts();
        }
    </script>
</body>

EDIT

Added the following code right before the </body>

<script type="text/javascript">
    if (google.accounts.user.checkLogin(scope))
    {
        setupContactsService();
        getMyContacts();
    }
</script>


@James Hill: Yes, it's working when run from a server. There is a small bug though:- When you run it for the first time, it doesn't return contacts. Try calling 'logMeOut()' just before you invoke 'initFunc()'. It will just go to the 'Grant Access' page and come back but not display any contacts. Please fix that :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜