如何获取gmail用户的联系人? [英] How to get gmail user's contacts?

查看:47
本文介绍了如何获取gmail用户的联系人?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检索用户存储在其 gmail 帐户中的电子邮件地址.在我的应用中,用户现在可以决定邀请他的朋友.我希望应用程序(如果用户告诉我好的")显示存储在 gmail 中的用户联系人电子邮件地址列表,他可以在其中选择一个或多个...

I need to retrieve the email addresses that the user has stored in his gmail account. In my app, the user can now decide to invite a friend of him. I want that the application (if the user tell me "ok") presents a list of the user's contacts email addresses stored in gmail, among which he can choose one or more...

我知道存在 Google API 的身份验证和授权". 方法正确吗?还有,如何在 Android 中使用它们?

I know that exists Authentication and Authorization for Google APIs". Is it the right way? And, how to use them in Android?

推荐答案

我希望这对像我这样的人有所帮助,因为我为此搜索了很多,最终完成了以下内容.

我已经将 GData java 客户端库用于 Google 通讯录 API v3.

I hope this will help for someone like me, because I have searched a lot for this and finally done with the below.

I have used GData java client library for Google Contacts API v3.

package com.example.cand;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;

import com.google.gdata.client.Query;
import com.google.gdata.client.Service;
import com.google.gdata.client.contacts.ContactsService;
import com.google.gdata.data.Link;
import com.google.gdata.data.contacts.ContactEntry;
import com.google.gdata.data.contacts.ContactFeed;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.NoLongerAvailableException;
import com.google.gdata.util.ServiceException;

public class MainActivity extends Activity {
    private URL feedUrl;
    private static final String username="yourUsername";
    private static final String pwd="yourPassword";
    private ContactsService service;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String url = "https://www.google.com/m8/feeds/contacts/default/full";

        try {
            this.feedUrl = new URL(url);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        new GetTask().execute();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    private class GetTask extends AsyncTask<Void, Void, Void>{

        @Override
        protected Void doInBackground(Void... params) {
            service = new ContactsService("ContactsSample");
            try {
                service.setUserCredentials(username, pwd);
            } catch (AuthenticationException e) {
                e.printStackTrace();
            }
            try {
                queryEntries(); 
            } catch (Exception e) {
                e.printStackTrace();
            }

            return null;
        }

    }

    private void queryEntries() throws IOException, ServiceException{
        Query myQuery = new Query(feedUrl);
        myQuery.setMaxResults(50);
        myQuery.setStartIndex(1);
        myQuery.setStringCustomParameter("showdeleted", "false");
        myQuery.setStringCustomParameter("requirealldeleted", "false");
//      myQuery.setStringCustomParameter("sortorder", "ascending");
//      myQuery.setStringCustomParameter("orderby", "");


        try{
            ContactFeed resultFeed = (ContactFeed)this.service.query(myQuery, ContactFeed.class);
                for (ContactEntry entry : resultFeed.getEntries()) {
                    printContact(entry);
                }
                System.err.println("Total: " + resultFeed.getEntries().size() + " entries found");

        }
        catch (NoLongerAvailableException ex) {
            System.err.println("Not all placehorders of deleted entries are available");
        }

    }
    private void printContact(ContactEntry contact) throws IOException, ServiceException{
        System.err.println("Id: " + contact.getId());
        if (contact.getTitle() != null)
            System.err.println("Contact name: " + contact.getTitle().getPlainText());
        else {
            System.err.println("Contact has no name");
        }

        System.err.println("Last updated: " + contact.getUpdated().toUiString());
        if (contact.hasDeleted()) {
            System.err.println("Deleted:");
        }

        //      ElementHelper.printContact(System.err, contact);

        Link photoLink = contact.getLink("http://schemas.google.com/contacts/2008/rel#photo", "image/*");
        if (photoLink.getEtag() != null) {
          Service.GDataRequest request = service.createLinkQueryRequest(photoLink);

          request.execute();
          InputStream in = request.getResponseStream();
          ByteArrayOutputStream out = new ByteArrayOutputStream();
          RandomAccessFile file = new RandomAccessFile("/tmp/" + contact.getSelfLink().getHref().substring(contact.getSelfLink().getHref().lastIndexOf('/') + 1), "rw");

          byte[] buffer = new byte[4096];
          for (int read = 0; (read = in.read(buffer)) != -1; )
            out.write(buffer, 0, read);
          file.write(out.toByteArray());
          file.close();
          in.close();
          request.end();
        }

        System.err.println("Photo link: " + photoLink.getHref());
        String photoEtag = photoLink.getEtag();
        System.err.println("  Photo ETag: " + (photoEtag != null ? photoEtag : "(No contact photo uploaded)"));

        System.err.println("Self link: " + contact.getSelfLink().getHref());
        System.err.println("Edit link: " + contact.getEditLink().getHref());
        System.err.println("ETag: " + contact.getEtag());
        System.err.println("-------------------------------------------\n");
    }

}


所需的库文件:您可以从 此处获取这些 jar

  • gdata-client-1.0.jar
  • gdata-client-meta-1.0.jar
  • gdata-contacts-3.0.jar
  • gdata-contacts-meta-3.0.jar
  • gdata-core-1.0.jar
  • guava-11.0.2.jar

注意:在 AndroidManifest 文件中添加互联网权限.

Note: Add internet permission in AndroidManifest file.

<uses-permission android:name="android.permission.INTERNET"/>

这篇关于如何获取gmail用户的联系人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆