NEWS

[C#] Hướng dẫn đọc danh bạ Google sử dụng Google Contact API V3

[C#] Hướng dẫn đọc danh bạ Google sử dụng Google Contact API V3
Đăng bởi: Thảo Meo - Lượt xem: 5934 07:54:56, 19/04/2019C#   In bài viết

Xin chào các bạn, bài viết hôm nay mình sẽ tiếp tục chia sẽ và hướng dẫn các bạn cách đọc danh bạ email, điện thoại từ Google, sử dụng Google Contact API V3.

[C#] Đọc danh bạ từ Google sử dụng Google Contact API V3

Google Contact chắc không còn xa lạ với các bạn. Hiện các bạn nào đang sử dụng điện thoại Android thì thường đồng bộ danh bạ từ điện thoại lên Google Contact.

Và trong bài viết này mình sẽ sử dụng ngôn ngữ lập trình C#, để đọc danh bạ từ Email Google về ListView.

Giao diện Google Contact của Gmail của mình trên Google.

google_contact_hinh1

Và dưới đây là giao diện ứng dụng đọc danh bạ từ email của mình về Listview trong C#:

Bao gồm các thông tin cơ bản: Tên danh bạ, địa chỉ email và số điện thoại.

google_contact_hinh2

Hướng dẫn thực hiện:

Đầu tiên, từ thư viện Nuget Console các bạn cài đặt cho mình hai thư viện bên dưới vào.

Install-Package Google.Apis.Auth
Install-Package Google.GData.Contacts

Và dưới đây là Full Source code Read Contact Google Api V3 C#:

using Google.Apis.Auth.OAuth2;
using Google.Apis.Util.Store;
using Google.Contacts;
using Google.GData.Client;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Google_Contact
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public  void auth()
        {

            string clientId = "461939640628-e514anaba9m17gaf215t272r11gd9dmo.apps.googleusercontent.com";
            string clientSecret = "seUvbPkQXC3-9avB0QXfAN_B";


            string[] scopes = new string[] { "https://www.googleapis.com/auth/contacts.readonly" };  
            try
            {
                // Use the current Google .net client library to get the Oauth2 stuff.
                UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId, ClientSecret = clientSecret }
                                                                                             , scopes
                                                                                             , "googleContact"
                                                                                           
                                                                                             , CancellationToken.None
                                                                                             , new FileDataStore("googleContact")).Result;

                // Translate the Oauth permissions to something the old client libray can read
                OAuth2Parameters parameters = new OAuth2Parameters();
               
                parameters.AccessToken = credential.Token.AccessToken;
                parameters.RefreshToken = credential.Token.RefreshToken;
              
                RunContactsSample(parameters);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
       
        private  void RunContactsSample(OAuth2Parameters parameters)
        {
            try
            {
                RequestSettings settings = new RequestSettings("Google contacts tutorial", parameters);
                ContactsRequest cr = new ContactsRequest(settings);
                cr.Settings.PageSize = 1000;
                var f = cr.GetContacts();
                int i = 0;
                    ListViewItem item = new ListViewItem();
                foreach (Contact c in f.Entries)
                {
                    i++;                   
                    var name = c.Name.FullName;
                    var email = (from xemail in c.Emails  select xemail.Address).FirstOrDefault();
                    var phone = (from xphone in c.Phonenumbers  select xphone.Value).FirstOrDefault();

                    item = new ListViewItem(new string[] { i + ".", name, email, phone });
                   
                     listView1.Items.AddRange(new ListViewItem[] { item });
                }
            }
            catch (Exception a)
            {
                Console.WriteLine("A Google Apps error occurred.");
                Console.WriteLine();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            auth();
        }
    }
}

 

Video demo ứng dụng:

Thanks for watching!

DOWNLOAD SOURCE

 

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hướng dẫn đọc danh bạ Google sử dụng Google Contact API V3
Đăng bởi: Thảo Meo - Lượt xem: 5934 07:54:56, 19/04/2019C#   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.