Google Contact API错误:通过脚本加载请求超时。可能的原因:供稿网址不正确; Feed需要身份验证

时间:2011-06-21 18:18:31

标签: javascript api contacts google-api

嗨,我在Google Contact JavaScript API上有错误。从最后一天开始,代码工作正常。但它今天不起作用。不知道出了什么问题。 :(

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>

2 个答案:

答案 0 :(得分:2)

以下代码适用于我。在Chrome,Safari和FireFox中测试过。变更摘要:

  • 检查是否已登录,如果已登录,请勿尝试再次登录。
  • 删除google.setOnLoadCallback(initFunc);并替换为按钮点击事件。永远不应该在页面加载时调用initFunc。
  • 重要:如果页面上没有与您的网页在同一个域中加载的图片,则无法使用此功能(每个谷歌)。

以下是代码:

<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>

修改

</body>

之前添加了以下代码
<script type="text/javascript">
    if (google.accounts.user.checkLogin(scope))
    {
        setupContactsService();
        getMyContacts();
    }
</script>

答案 1 :(得分:0)

@James Hill: 是的,从服务器运行时它正在工作。但是有一个小虫子: - 当您第一次运行它时,它不会返回联系人。在调用'initFunc()之前尝试调用'logMeOut()'。它将转到“授权访问”页面并返回但不显示任何联系人。请修正:)