我正在创建一个用户可以使用Facebook登录的项目。我想根据Facebook登录检索用户详细信息。我正在获取Facebook Id,名字,姓氏,性别,区域设置和Facebook链接等详细信息,并将其存储在数据库中。我无法获得用户的电子邮件ID,IP地址和个人资料照片。对于配置文件pic,我给了varbinary(max)作为数据类型但是当我尝试将它保存到数据库时,我得到了错误 -
读取字节时出错。意外的令牌:StartObject。第1行,位置 191。
我的编码如下:
FacebookUserModel.cs:
public partial class FacebookUserModel
{
public int UserId { get; set; }
public string id { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public string gender { get; set; }
public string locale { get; set; }
public string link { get; set; }
public byte[] picture { get; set; }
}
AccountController.cs:
public class AccountController : Controller
{
private DetailsFBEntities1 db = new DetailsFBEntities1();
//
// GET: /Account/Login
[HttpPost]
public JsonResult FacebookLogin(FacebookLoginModel model)
{
Session["uid"] = model.uid;
Session["accessToken"] = model.accessToken;
Session["ip"] = Request.UserHostAddress;
return Json(new { success = true });
}
[HttpGet]
public ActionResult UserDetails(FacebookUserModel umodel)
{
try
{
string ip = Request.UserHostAddress;
var client = new FacebookClient(Session["accessToken"].ToString());
//byte[] data = System.IO.File.ReadAllBytes("umodel.picture");
dynamic fbresult = client.Get("me?fields=id,first_name,last_name,gender,locale,link,picture");
FacebookUserModel facebookUser = Newtonsoft.Json.JsonConvert.DeserializeObject<FacebookUserModel>(fbresult.ToString());
if (ModelState.IsValid)
{
db.FacebookUserModels.Add(facebookUser);
db.SaveChanges();
}
return View(facebookUser);
}
catch (DbEntityValidationException e)
{
foreach (var eve in e.EntityValidationErrors)
{
Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
eve.Entry.Entity.GetType().Name, eve.Entry.State);
foreach (var ve in eve.ValidationErrors)
{
Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
ve.PropertyName, ve.ErrorMessage);
}
}
throw;
}
}
}
Facebook.js:
function InitialiseFacebook(appId) {
window.fbAsyncInit = function () {
FB.init({
appId: appId,
status: true,
cookie: true,
xfbml: true
});
FB.Event.subscribe('auth.login', function (response) {
var credentials = { uid: response.authResponse.userID, accessToken: response.authResponse.accessToken };
SubmitLogin(credentials);
});
FB.getLoginStatus(function (response) {
if (response.status === 'connected') {
alert("user is logged into fb");
}
else if (response.status === 'not_authorized') { alert("user is not authorised"); }
else { alert("user is not conntected to facebook"); }
});
function SubmitLogin(credentials) {
$.ajax({
url: "/account/facebooklogin",
type: "POST",
data: credentials,
error: function () {
alert("error logging in to your facebook account.");
},
success: function () {
window.location.reload();
}
});
}
};
(function (d) {
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {
return;
}
js = d.createElement('script');
js.id = id;
js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
}
UserDetails.cs:
<table>
<tr><td>Picture</td><td><img src='@Model.picture' /></td></tr>
@Html.HiddenFor(model => model.UserId)
<tr><td>Facebook Id</td><td>@Model.id</td></tr>
<tr><td>First Name</td><td>@Model.first_name</td></tr>
<tr><td>Last Name</td><td>@Model.last_name</td></tr>
<tr><td>Gender</td><td>@Model.gender</td></tr>
<tr><td>Locale</td><td>@Model.locale</td></tr>
<tr><td>Facebook Link</td><td>@Model.link</td></tr>
</table>
我是新手,所以请帮忙。
答案 0 :(得分:2)
我把图片作为varchar(max)而不是varbinary(max),并按如下方式分配值:
FacebookUserModel facebookUser = new FacebookUserModel
{
first_name = fbresult.first_name,
gender = fbresult.gender,
id = fbresult.id,
last_name = fbresult.last_name,
locale = fbresult.locale,
link = fbresult.link,
picture = fbresult.picture.data.url,
};
因此,在内部图片中,我们只获取该图片的网址,我们可以在任何我们想要的地方使用它。