hung pro vn

[C#] Instructions to read Google contacts using Google Contact API V3

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.
 
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.

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.

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:


DOWNLOAD SOURCE CODE


Password Unzip : hungqb.com

Hướng dẫn bình luận

Mọi người để lại bình luận góp ý, nhận xét về những bài viết mà mình chia sẽ văn minh lịch sự hay kích động, Không spam, không chèn link quảng cáo, bán hàng, Không sử dụng từ ngữ thô tục, xúc phạm, kích động, Link chỉ được phép khi thực sự liên quan đến nội dung bài viết, Không mạo danh người khác hoặc sử dụng email giả, Bình luận vi phạm sẽ bị xóa không cần thông báo trước.
Mọi người lưu ý răng, nếu muốn chia sẽ code ở bình luần thì cần mã hóa code trước khi bỏ vào khung nhé. :)
⑴ Chèn ℂ𝕤𝕤 theo mẫu : [pre css] CSS [/pre]
⑵ Chèn ℍ𝕥𝕞𝕝 theo mẫu : [pre html] HTML [/pre]
⑶ Chèn 𝕁𝕒𝕧𝕒𝕤𝕔𝕣𝕚𝕡𝕥 theo mẫu : [pre js] Javascript [/pre]

🖼️ Chèn 𝕀𝕞𝕒𝕘𝕖 theo mẫu : [img] Link hình ảnh [/img]
🎞️ Chèn Video 𝕐𝕠𝕦𝕥𝕦𝕓𝕖 theo mẫu : [youtube] Link Youtube [/youtube]

1 Nhận xét

  1. tiện ích này tốt nhé mọi ng, mọi ng cùng nhau phát triển và tối ưu danh bạ cá nhân thật tốt nhé <3

    Trả lờiXóa

Top