所以我对ASP.NET MVC和Windows Live Connect API还比较新。基本上,我正在尝试将Live登录集成到我的网站中。当用户登录时,Live会请求他们的权限以提供我的应用程序某些信息,并将用户发送到指定了查询字符串的重定向uri。在这里,如果用户是第一次登录网站,我希望将他们的基本信息存储在我的服务器上(名字,姓氏,电子邮件)。我已经能够获取他们的名字和姓氏,但很难找到如何检索他们的主要电子邮件地址。我将解释我目前所做的事情。
我找不到最好的方法将Live connect集成到MVC应用程序中,所以我猜测了一下。我在我的重定向uri中指定了一个控制器操作,以获取查询字符串“code”来构建HTTP Post。
我找不到最好的方法将Live connect集成到MVC应用程序中,所以我猜测了一下。我在我的重定向uri中指定了一个控制器操作,以获取查询字符串“code”来构建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调用,如下所示。
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字符串:
{
"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调用才能检索电子邮件地址?
另外,我这样做对吗?