嗨,我在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>
答案 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()'。它将转到“授权访问”页面并返回但不显示任何联系人。请修正:)