谷歌联系v3api&;OAuth v2,Java语言

谷歌联系v3api&;OAuth v2,Java语言,java,oauth-2.0,google-contacts-api,Java,Oauth 2.0,Google Contacts Api,在过去的几天里,我一直在尝试使用上述API获取google联系人列表。 可以说,没有成功。 关于我的问题,谷歌文档(如果我可以说是一团糟的话)并没有太大帮助。 问题是,我不知道如何使用OAuth v2 API授权ContactsService对象。 我已经下载了GoogleOAuth2.0库,对于像我这样的初学者来说,它同样没有合适的文档和/或合适的示例 总之,对于上述问题,有人有任何“Hello world”类型的示例或任何类型的“指南”吗 作为旁注,我确实使用Scribe API获得了联系人

在过去的几天里,我一直在尝试使用上述API获取google联系人列表。 可以说,没有成功。 关于我的问题,谷歌文档(如果我可以说是一团糟的话)并没有太大帮助。 问题是,我不知道如何使用OAuth v2 API授权ContactsService对象。 我已经下载了GoogleOAuth2.0库,对于像我这样的初学者来说,它同样没有合适的文档和/或合适的示例

总之,对于上述问题,有人有任何“Hello world”类型的示例或任何类型的“指南”吗

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


谢谢

我在检索谷歌联系人时也遇到了麻烦。 使用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 (...) { ... }
}
最终,您将能够使用组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 (...) { ... }
}
private静态最终字符串联系人\u URL=”https://www.google.com/m8/feeds/contacts/default/full";
专用静态最终int MAX_NB_CONTACTS=1000;
私有列表getContacts(){
ContactsService ContactsService=新联系人服务(“我的产品名称”);
contactsService.setHeader(“授权”、“承载人”+我的访问令牌);
试一试{
URL feedUrl=新URL(联系人地址);
Query myQuery=新查询(feedUrl);
//检索我在上面找到的组的联系人
myQuery.SetString自定义参数(“组”,组);
myQuery.setMaxResults(最大联系人);
ContactFeed resultFeed=contactsService.query(myQuery,ContactFeed.class);
List contactEntries=resultFeed.getEntries();
返回联系人条目;
}捕获(…){…}
}

看来我终于取得了一些进展。 很明显,问题是有很多不同的OAuth2库,其中一些已经被弃用,或者无法与Contacts v3一起使用,也就是说,生成的访问令牌将无效(这就是我的结论)

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

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

Servlet 2(处理访问令牌)

protectedvoidprocessrequest(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException{
setContentType(“text/html;charset=UTF-8”);
PrintWriter out=response.getWriter();
out.println(“”);
out.println(“”);
out.println(“Servlet SignInFinished”);
out.println(“”);
out.println(“”);
HttpTransport传输=新的NetHttpTransport();
JsonFactory JsonFactory=新的JacksonFactory();
GoogleAuthorizationCodeTokenRequest authorizationTokenRequest=新的GoogleAuthorizationCodeTokenRequest(传输、jsonFactory、客户机ID、客户机机密、request.getParameter(“代码”),重定向URL);
GoogleTokenResponse tokenResponse=authorizationTokenRequest.execute();
println(“OAuth2访问令牌:+tokenResponse.getAccessToken());
GoogleCredential gc=新的GoogleCredential();
gc.setAccessToken(tokenResponse.getAccessToken());
ContactsService ContactsService=新ContactsService(“套索项目”);
contactsService.setOAuth2Credentials(gc);
试一试{
URL feedUrl=新URL(“https://www.google.com/m8/feeds/contacts/default/full");
Query myQuery=新查询(feedUrl);
myQuery.setMaxResults(1000);
ContactFeed resultFeed=contactsService.query(myQuery,ContactFeed.class);
for(int i=0;i”);
}
}捕获(例外e){
系统输出打印ln(e);
}
out.println(“”);
out.println(“”);
}
注意: 若您正在为Web应用程序使用客户端ID,则重定向URL必须是您在通过Google控制台注册应用程序时输入的重定向URL之一


嗯,我希望这会对你们中的一些人有所帮助:)。

已经尝试过这个方法,就像以前一样,query/getFeed方法引发了一个异常:
java.lang.NullPointerException:No authentication header information
它对我有效。我猜你的问题来自OAuth部分。你确定你的访问令牌有效吗?我得到了访问令牌,它有效吗?不确定是否有办法检查:例如,对于一个有效的访问令牌,您会得到类似于{“访问令牌”:“1/fFAGRNJru1FTz70BzhT3Zg”,“expires_in”:3920,“令牌类型”:“Bearer”}的内容。我不知道您正在开发什么样的项目,但检查您的访问令牌是否正确并不难,因为它是一个java web应用程序。使用2个servlet,一个用于初始化auth进程,另一个作为回调servlet,我在其中获取访问令牌。使用
GoogleOAuthParameters
设置参数,使用
GoogleOAuthHelper
生成验证url。我想在这里粘贴我的代码,但我可以输入的字符数是有限的。非常感谢。经过多次失败的尝试,我终于在你的帮助下成功了!Cotheer先生,您能告诉我您用于联系服务的jar版本吗?很好的例子。非常感谢你。使用这个API,这么多不同的版本和方法,让人非常沮丧。你的是最简单的,最接近我需要的。真是太好了。
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());

}
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>");
}