get Gmail contacts on button click.
1) our project should be registered in GoogleContact API console so you can get CustomerKey(ClientId), CustomerSecretKey(ClientSecretKey)
2) follow the sample code below:
protected void btnInviteFriends_Click(object sender, EventArgs e)
{
const string clientID = "Your Client Id";
const string clientSecret = "YourClientSecret";
const string redirectUri = "https://YourDomain/Default.aspx";
AuthorizationServerDescription server = new AuthorizationServerDescription
{
AuthorizationEndpoint = new Uri("https://accounts.google.com/o/oauth2/auth"),
TokenEndpoint = new Uri("https://accounts.google.com/o/oauth2/token"),
ProtocolVersion = ProtocolVersion.V20,
};
List<string> scope = new List<string>
{
"https://mail.google.com/",
"https://www.googleapis.com/auth/userinfo#email"
};
WebServerClient consumer = new WebServerClient(server, clientID, clientSecret);
consumer.RequestUserAuthorization(scope, new Uri(redirectUri));
consumer.ClientCredentialApplicator =
ClientCredentialApplicator.PostParameter(clientSecret);
IAuthorizationState grantedAccess = consumer.ProcessUserAuthorization(null);
string accessToken = grantedAccess.AccessToken;
GoogleApi api = new GoogleApi(accessToken);
string user = api.GetEmail();
using (Imap imap = new Imap())
{
imap.ConnectSSL("imap.gmail.com");
imap.LoginOAUTH2(user, accessToken);
imap.SelectInbox();
List<long> uids = imap.Search(Flag.Unseen);
foreach (long uid in uids)
{
string eml = imap.GetMessageByUID(uid);
IMail email = new MailBuilder().CreateFromEml(eml);
Console.WriteLine(email.Subject);
}
imap.Close();
}
AuthorizationServerDescription authServer = new AuthorizationServerDescription
{
AuthorizationEndpoint =
new Uri("https://accounts.google.com/o/oauth2/auth?access_type=offline"),
};
grantedAccess = consumer.ProcessUserAuthorization(null);
consumer.RefreshAuthorization(grantedAccess, TimeSpan.FromMinutes(20));
authServer = new AuthorizationServerDescription
{
AuthorizationEndpoint =
new Uri("https://accounts.google.com/o/oauth2/auth?access_type=offline&approval_prompt=force"),
};
}