点击按钮即可将Gmail联系人导入我的网站 [英] Importing Gmail contacts to my website on a button click
本文介绍了点击按钮即可将Gmail联系人导入我的网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个任务是获取登录用户的gmail联系人并将其显示给他。
我我已经了解了如何实现它。
请帮我解决这个问题。
要求:
1)当我点击一个按钮时,它应该要求登录gmail。
2)如果用户登录成功,则应重新启动并显示所有联系人。
问候,
Rohith
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
推荐答案
请参阅 https: //developers.google.com/google-apps/contacts/v3/?hl=en [ ^ ]。
您的要求的第一部分必须由您自己完成。第二部分使用此代码。 [ ^ ]
First part of your requirement has to be done by yourself. For second part Use this code.[^]
点击按钮获取Gmail联系人。
1)我们的项目应该在GoogleContact API控制台中注册,这样您就可以获得CustomerKey(ClientId),CustomerSecretKey(ClientSecretKey) )
2)按照下面的示例代码:
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"),
};
}
这篇关于点击按钮即可将Gmail联系人导入我的网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文