Click here to Skip to main content
15,895,813 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hi,

I have a task to get the gmail contacts of a logged in user and display it to him.
I have gone through the Google Developers Portal but I did get the idea how to implement it.
Please help me in solving this.

Requirements:

1) when I click on a button it should ask for gmail login.

2) If the user login is successful then all the contacts should be retrived and displayed.


Regards,
Rohith
Posted
Updated 28-Oct-13 23:26pm
v2

First part of your requirement has to be done by yourself. For second part Use this code.[^]
 
Share this answer
 
Comments
Rohith Reddy Vadiyala 30-Oct-13 7:37am    
Hi Zafar,

Thanks for the response. But what I'm actually looking for is " Importing Contacts with Google Authentication".
Which means I should navigate to google accounts page, login with gmail credintials and on success the contacts should be fetched.
Please help me in sorting this out.

Regards,
Rohith.
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";
// Page url to which           it should be redirected after accessing Contact API
            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"
                 //GoogleScope.ImapAndSmtp.Name,
                 //GoogleScope.EmailAddressScope.Name 
            };

            //At this point user is redirected to Google to authorize the access:
            WebServerClient consumer = new WebServerClient(server, clientID, clientSecret);
            // Here redirect to authorization site occurs
            consumer.RequestUserAuthorization(scope, new Uri(redirectUri));

            //After this step user is redirected back to your website (ReDirect URL). Following is this callback code. Its purpose is to get a refresh-token and an access-token:

            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();
            }


            //Refreshing access token
            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"),
            };

        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900