在ASP.NET中使用实时连接API来获取用户的电子邮件地址 [英] Using Live Connect API in ASP.NET to retrieve a user's email address

查看:308
本文介绍了在ASP.NET中使用实时连接API来获取用户的电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我是相当新的ASP.NET MVC和Windows Live连接API。基本上,我想活在符号融入到我的网站。当用户登录,现场要求其许可给我的应用程序的某些信息,将用户在查询字符串追加我的应用程序设置中指定重定向URI。在这里,如果用户登录到该网站的第一次,我想他们的基本信息存储在我的服务器(姓,名,电子邮件)上。我已经能够得到他们的第一个和最后一个名字,但我有一个很难找出如何找回自己的主电子邮件地址。我要解释一下我到目前为止已经完成。

So I am fairly new to ASP.NET MVC and Windows Live Connect API. Basically I am trying to integrate Live sign in into my website. When users sign in, Live requests their permission to give my app certain info, sends the user to a redirect uri specified in my app settings appended with a querystring. Here, if the user is signing into the site for the first time, I want their basic info stored on my server (first name, last name, email). I have been able to get their first and last name, but am having a hard time finding out how to retrieve their primary email address. I'll explain what I've done so far.

我找不到整合实时连接成一个MVC应用程序,所以我把我最好的猜测的最好办法。我在指定的重定向我的URI的控制器动作,是以查询字符串code构建一个HTTP POST。

I couldn't find the best way to integrate Live connect into an MVC app so I took my best guess. I specified a controller action in my redirect uri that takes the querystring "code" to construct an HTTP Post.

HttpRequest req = System.Web.HttpContext.Current.Request;

            string myAuthCode = req.QueryString["code"];
            string myAppId = ConfigurationManager.AppSettings.Get("wll_appid");
            string mySecret = ConfigurationManager.AppSettings.Get("wll_secret");
            string postData = "client_id=" + myAppId + "&redirect_uri=http%3A%2F%2Fmscontestplatformtest.com%2FContestPlatform%2FUser%2FSignIn&client_secret=" + mySecret + "&code=" + myAuthCode + "&grant_type=authorization_code";

            byte[] byteArray = Encoding.UTF8.GetBytes(postData);

            WebRequest request = WebRequest.Create("https://oauth.live.com/token");
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = byteArray.Length;
            request.Method = "POST";

获取JSON格式的字符串响应和提取的access_token。然后,我用这个访问令牌如下构建一个HTTP GET调用。

Get the JSON-formatted string response and extract the access_token. Then I use this access token to construct an HTTP GET call as follows.

request = WebRequest.Create("https://apis.live.net/v5.0/me?access_token=" + r.access_token);
            response = request.GetResponse();
            reader = new StreamReader(response.GetResponseStream());

            string userInfo = reader.ReadToEnd(); 

上面的GET电话为我提供了这个JSON字符串:

The GET call above provides me with this JSON string:

{ 
    "id": "02b4b930697bbea1", 
    "name": "Daniel Hines", 
    "first_name": "Daniel", 
    "last_name": "Hines", 
    "link": "http://profile.live.com/cid-02b4b930697bbea1/", 
    "gender": "male", 
    "locale": "en_US", 
    "updated_time": "2011-10-14T21:40:38+0000" 
} 

这就是所有的公共信息和是伟大的,我几乎我需要的一切只是他们的主电子邮件地址。什么样的GET调用的,我需要检索电子邮件?

Which is all public info and is great, I have almost everything I need except their primary email address. What sort of GET call do I need to retrieve email?

另外,我在做这个正确的方式?

Also, am I doing this right way?

推荐答案

好吧,我想通了!

我只需要的范围。wl.emails添加到我的登录链接。然后我的GET调用将返回他们的电子邮件地址。

I just needed to add the scope "wl.emails" to my sign in link. Then my GET call will return their email addresses.

这篇关于在ASP.NET中使用实时连接API来获取用户的电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆