- [DEVEXPRESS] Hướng dẫn bật tính năng Scroll Pixcel in Touch trên GridView
- [DEVEXPRESS] Hướng dẫn sử dụng TileBar viết ứng dụng duyệt hình ảnh Winform
- [DEVEXPRESS] Tô màu border TextEdit trên Winform
- [C#] Lấy dữ liệu từ Console Write hiển thị lên textbox Winform
- [C#] Hiển thị Progress bar trên Window Console
- [C#] Di chuyển control Runtime và lưu layout trên winform
- [SQLSERVER] Sử dụng hàm NULL IF
- [C#] Chia sẽ source code mã đi tuần bằng giao diện Winform
- [C#] Flash Window in Taskbar Winform
- Download và Giải nén tập tin File sử dụng Powershell
- [C#] Hướng dẫn cách lấy thông tin đăng nhập tài khoản và mật khẩu web browser trên windows
- [VB.NET] CRUD Thêm xóa sửa tìm kiếm Realtime FireBase
- [C#] Hiển thị thông báo Toast Message trong lập trình Winform
- [C#] Cấu hình định dạng ngày tháng, thời gian trên Windows cho ứng dụng Winform
- [C#] Rút gọn đường dẫn link url với TinyURL API
- [C#] Hướng dẫn cách bo tròn winform with Radius
- [C#] Chia sẽ class BackGroundOverlay Show Modal cho Winform
- [C#] Hướng dẫn Flip Image Winform
- [C#] Invoke là gì? cách sử dụng phương thức Invoke()
- [C#] Hướng dẫn chia sẽ file, folder từ ứng dụng sang Zalo Chat
[C#] Hướng dẫn đọc danh bạ Google sử dụng 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.
[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.
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:
Thanks for watching!