2

我使用 Google (GDATA) Gmail API 从 gmail 中检索联系人列表,它在 windows 环境下成功运行,但是当我在 Linux 上运行相同的代码时,我收到 Invalid Credentials 的错误。google了一下,还是没啥帮助

这是我的代码

public static String getGmailContactList() {
    String response = "";
    StringBuilder str = new StringBuilder();
    String statusString = "";
    ArrayList list = new ArrayList();
    ContactsService myService = new ContactsService("");
    String email = "xxxxx@gmail.com";
    String password = "xxxxxxxx";
    try
    {
        try
        {
            myService.setUserCredentials(email, password);
        }
        catch (AuthenticationException ex)
        {
            ex.printStackTrace();
            //****I got exception here when using this code on LINUX ENVIORMENT** ***
        }
        response = printAllContacts(myService, email);
        Iterator itr = list.iterator();
        while (itr.hasNext())
        {
            ArrayList contact = (ArrayList) itr.next();
            try
            {
                str.append(contact.get(1)).append(",");
            }
            catch (Exception e)
            {
                log.debug("Exception ocurred inside fethching gmail contact >
                >
                >
                " + e);
                str.append("no contacts found");
            }
            str.substring(0, str.length() - 1);
        }
    }
    catch (Exception ae)
    {
        response = statusString;
        log.debug("Exception ocurred inside ReadContacts : getGmailContactList()" + ae);
    }
    return response;
}

public static String printAllContacts(ContactsService myService, String emailSent)//
throws ServiceException, IOException
{
    URL feedUrl = new URL("http://www.google.com/m8/feeds/contacts/" + emailSent + "/full");
    Query myQuery = new Query(feedUrl);
    myQuery.setMaxResults(100);
    ContactFeed resultFeed = myService.getFeed(myQuery, ContactFeed.class);
    String phones = null;
    String emails = null;
    log.debug(resultFeed.getTitle().getPlainText());
    StringBuilder contacts = new StringBuilder();
    contacts.append("<?xml version=\"1.0\"><Contacts>");
    for (int i = 0; i < resultFeed.getEntries().size(); i++)
    {
        contacts.append("<Contact>");
        ContactEntry entry = resultFeed.getEntries().get(i);
        if (entry.hasName())
        {
            Name name = entry.getName();
            if (name.hasFullName())
            {
                String fullNameToDisplay = name.getFullName().getValue();
                if (name.getFullName().hasYomi())
                {
                    fullNameToDisplay += " (" + name.getFullName().getYomi() + ")";
                }
                contacts.append("<Name>").append(fullNameToDisplay).append("</Name>");
            }
            else
            {
                contacts.append("<Name>").append("").append("</Name>");
            }
        }
        else
        {
            contacts.append("<Name>").append("").append("</Name>");
        }
        StringBuilder emailIds = new StringBuilder();
        if (entry.hasEmailAddresses())
        {
            List<Email> email = entry.getEmailAddresses();
            if (email != null && email.size() > 0)
            {
                for (Email e : email)
                {
                    emailIds.append(e.getAddress()).append(",");
                }
                emailIds.trimToSize();
                if (emailIds.indexOf(",") != -1)
                {
                    emails = emailIds.substring(0, emailIds.lastIndexOf(","));
                }
                contacts.append("<Email>").append(emails).append("</Email>");
            }
            else
            {
                contacts.append("<Email>").append("").append("</Email>");
            }
        }
        else
        {
            contacts.append("<Email>").append("").append("</Email>");
        }
        contacts.append("</Contact>");
    }
    contacts.append("</Contacts>");
    return contacts.toString();
}

所以在我落后的地方,会得到某种帮助

这是堆栈跟踪

com.google.gdata.client.GoogleService$InvalidCredentialsException:凭据无效
        在 com.google.gdata.client.GoogleAuthTokenFactory.getAuthException(GoogleAuthTokenFactory.java:660)
        在 com.google.gdata.client.GoogleAuthTokenFactory.getAuthToken(GoogleAuthTokenFactory.java:560)
        在 com.google.gdata.client.GoogleAuthTokenFactory.setUserCredentials(GoogleAuthTokenFactory.java:397)
        在 com.google.gdata.client.GoogleService.setUserCredentials(GoogleService.java:364)
        在 com.google.gdata.client.GoogleService.setUserCredentials(GoogleService.java:319)
        在 com.google.gdata.client.GoogleService.setUserCredentials(GoogleService.java:303)
        在 com.gmail.ReadContacts.getGmailContactList(ReadContacts.java:55)
4

0 回答 0