Java联系人v3 API和OAuth v2

Cot*_*eer 10 java google-contacts-api oauth-2.0

在过去的几天里,我一直试图使用上述API获取谷歌联系人列表.针说,没有成功.谷歌文档(如果我可以说,这是一个完全混乱)对我的问题不是很有帮助.问题是,我不知道如何使用OAuth v2 API授权ContactsService对象.我已经下载了Google OAuth2.0库,对于像我这样的初学者来说,它没有适当的文档和/或没有适当的示例.

总而言之,对于上述问题,是否有人有任何工作"Hello world"类型的示例或任何类型的"指导"?

作为旁注,我确实设法使用Scribe API获取联系人,但是您可能知道,响应是xml/json格式,需要首先解析,这不是我想要的.

谢谢

Cot*_*eer 8

看来我终于取得了一些进展.显然,问题在于存在大量不同的OAuth2库,其中一些已被弃用或者不能与Contacts v3一起使用,也就是说,生成的访问令牌将无效(这就是我的结论).

因此,对于授权和生成访问令牌,我使用了Google API Client 1.14.1(测试版),我的代码如下:

Servlet 1(生成身份验证URL):

protected void processRequest(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");           

        GoogleAuthorizationCodeRequestUrl authorizationCodeURL=new GoogleAuthorizationCodeRequestUrl(CLIENT_ID, REDIRECT_URL, SCOPES);

        authorizationCodeURL.setAccessType("offline");//For future compatibility

        String authorizationURL=authorizationCodeURL.build();
        System.out.println("AUTHORIZATION URL: "+authorizationURL); 

        response.sendRedirect(new URL(authorizationURL).toString());

}
Run Code Online (Sandbox Code Playgroud)

Servlet 2(处理访问令牌)

protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();


    out.println("<html>");
    out.println("<head>");
    out.println("<title>Servlet SignInFinished</title>");
    out.println("</head>");
    out.println("<body>");


    HttpTransport transport = new NetHttpTransport();
    JsonFactory jsonFactory = new JacksonFactory();
    GoogleAuthorizationCodeTokenRequest authorizationTokenRequest = new GoogleAuthorizationCodeTokenRequest(transport, jsonFactory, CLIENT_ID, CLIENT_SECRET, request.getParameter("code"), REDIRECT_URL);

    GoogleTokenResponse tokenResponse = authorizationTokenRequest.execute();

    out.println("OAuth2 Access Token: " + tokenResponse.getAccessToken());

    GoogleCredential gc = new GoogleCredential();
    gc.setAccessToken(tokenResponse.getAccessToken());

    ContactsService contactsService = new ContactsService("Lasso Project");
    contactsService.setOAuth2Credentials(gc);

    try {
        URL feedUrl = new URL("https://www.google.com/m8/feeds/contacts/default/full");

        Query myQuery = new Query(feedUrl);
        myQuery.setMaxResults(1000);

        ContactFeed resultFeed = contactsService.query(myQuery, ContactFeed.class);

        for (int i = 0; i < resultFeed.getEntries().size(); i++) {
            out.println(resultFeed.getEntries().get(i).getTitle().getPlainText() + "<br/>");
        }

    } catch (Exception e) {
        System.out.println(e);
    }

    out.println("</body>");
    out.println("</html>");
}
Run Code Online (Sandbox Code Playgroud)

注意: 如果您使用Web应用程序的客户端ID,则REDIRECT_URL必须是您在通过Google控制台注册应用程序时输入的重定向URL之一.

好吧,我希望这对你们中的一些人有所帮助:).


its*_*tsu 0

我在尝试检索谷歌联系人时也遇到了麻烦。使用OAuth2.0,您首先需要获取访问令牌。 然后就很简单了,你可以请求你正在寻找的组的ID :

import com.google.gdata.client.contacts.ContactsService;
import com.google.gdata.data.contacts.ContactEntry;
import com.google.gdata.data.contacts.ContactFeed;
import com.google.gdata.data.contacts.ContactGroupEntry;
import com.google.gdata.data.contacts.ContactGroupFeed;

private static final String GROUPS_URL = "https://www.google.com/m8/feeds/groups/default/full";

private int getIdOfMyGroup() {
    ContactsService contactsService = new ContactsService("MY_PRODUCT_NAME");
    contactsService.setHeader("Authorization", "Bearer " + MY_ACCESS_TOKEN);

    try {
        URL feedUrl = new URL(GROUPS_URL);
        ContactGroupFeed resultFeed = contactsService.getFeed(feedUrl, ContactGroupFeed.class);
        // "My Contacts" group id will always be the first one in the answer
        ContactGroupEntry entry = resultFeed.getEntries().get(0);

        return entry.getId();
    } catch (...) { ... }
}
Run Code Online (Sandbox Code Playgroud)

最终您将能够使用组 ID 来请求其联系人:

private static final String CONTACTS_URL = "https://www.google.com/m8/feeds/contacts/default/full";
private static final int MAX_NB_CONTACTS = 1000;

private List<ContactEntry> getContacts() {
    ContactsService contactsService = new ContactsService("MY_PRODUCT_NAME");
    contactsService.setHeader("Authorization", "Bearer " + MY_ACCESS_TOKEN);

    try {
        URL feedUrl = new URL(CONTACTS_URL);
        Query myQuery = new Query(feedUrl);
                    // to retrieve contacts of the group I found just above
        myQuery.setStringCustomParameter("group", group);
        myQuery.setMaxResults(MAX_NB_CONTACTS);
        ContactFeed resultFeed = contactsService.query(myQuery, ContactFeed.class);
        List<ContactEntry> contactEntries = resultFeed.getEntries();

        return contactEntries;
    } catch (...) { ... }
}
Run Code Online (Sandbox Code Playgroud)