Notification texts go here Contact Us Buy Now!

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

[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

About the Author

Chia sẽ kiến thức và tư duy sáng tạo của bản thân mỗi ngày cho cộng đồng IT Việt Nam và Thế giới. Chia sẽ thủ thuật lập trình Csharp, Visual Basic, Python, Php, Javascript, Css, Html. Tối ưu hóa Windows, Microsoft, Facebook, Youtube, Chrome...v.v. …

1 comment

  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
Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.
NextGen Digital Welcome to WhatsApp chat
Howdy! How can we help you today?
Type here...