Google联系人API错误:通过脚本加载请求超时.可能的原因:源URL不正确;提要需要身份验证

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

本文关键字:URL 不正确 身份验证 错误 API 联系人 脚本 Google 超时 请求 加载      更新时间:2023-09-26

嗨,我在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>

以下代码适用于我。在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>

@James Hill:是的,在服务器上运行时可以正常工作。不过有一个小错误:-当你第一次运行它时,它不会返回联系人。请尝试在调用"initFunc()"之前调用"logMeOut()"。它只会转到"授予访问权限"页面并返回,但不会显示任何联系人。请修复:)