2011-11-03 129 views
0

我正在使用Facebook C#SDK,並獲得了有關如何設置登錄並獲取一些用戶信息以在我的網站上註冊的示例。但是,我卡住了電子郵件信息。它始終爲空。Facebook C#SDK,獲取用戶電子郵件的問題

我檢查了一些在這裏相關的帖子,但沒有解決我的問題。我需要的所有信息都是正確的,只有電子郵件和生日不見了。

我在SHTML部分頁面代碼是這樣的:

<div id="fb-root"></div> 
<fb:login-button id="fblogin" onlogin="window.open('http://' + location.host + '/Facebook/LogOn', '_self')" perms='email'></fb:login-button> 
<script> 
    window.fbAsyncInit = function() { 
     FB.init({ 
      appId: '@Facebook.FacebookApplication.Current.AppId', 
      cookie: true, 
      xfbml: true, 
      oauth: true 
     }); 

     function facebooklogin() { 
      FB.login(function (response) { 
       if (response.authResponse) { 
        // user authorized 
        //window.location.reload(); 
        window.open("http://" + location.host + "/Facebook/LogOn", "_self") 
       } else { 
        // user cancelled 
       } 
      }, { scope: '@ViewBag.ExtendedPermissions' }); 
     }; 

     $(function() { 
      // make the button is only enabled after the facebook js sdk has been loaded. 
      $('#fblogin').attr('disabled', false).click(facebooklogin); 
     }); 
    }; 
    (function() { 
     var e = document.createElement('script'); e.async = true; 
     e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js'; 
     document.getElementById('fb-root').appendChild(e); 
    }()); 
</script> 

和我的Facebook控制器代碼:

public ActionResult LogOn() 
{ 
    var fbWebContext = new FacebookWebContext(); // or FacebookWebContext.Current; 

    if (fbWebContext.IsAuthorized()) 
    { 
     var fb = new FacebookWebClient(); 
     dynamic me = fb.Get("me"); 

     //Creating new User with Facebook credentials 
     var id = 0; 
     var nickname = me.first_name; 
     var password = ""; 
     var email = me.email; 
     var picture = string.Format("http://graph.facebok.com/{0}/picture?type=large", me.id); 
     var thumbnail = string.Format("http://graph.facebok.com/{0}/picture?type=normal", me.id); 
     var fullName = me.name; 
     var gender = ""; 
     if (me.gender == "male") gender = "Masculino"; 
     if (me.gender == "female") gender = "Feminino"; 
     var birthdate = Convert.ToDateTime(me.birthday); 

     //Does the user already exist? 
     if (DAUser.UserAlreadyExists(email)) 
     { 
      ViewBag.Message = "O seu e-mail já foi cadastrado em nosso site. Por favor, cadastre outro e-mail."; 
      return View("~/Views/Shared/Erro.cshtml"); 
     } 
     else 
     { 
      id = DAUser.Create(nickname, email, password, "facebook"); 
     } 

     //Updating created user information 
     User user = db.User.Find(id); 
     TryUpdateModel(user); 
     user.Picture = picture; 
     user.Thumbnail = thumbnail; 
     user.Nickname = nickname; 
     user.FullName = fullName; 
     user.Gender = gender; 
     if (!String.IsNullOrEmpty(birthdate)) 
     { 
      user.Birthdate = Convert.ToDateTime(birthdate); 
     } 

     db.SaveChanges(); 
     Session["loginType"] = "Facebook"; 

     return RedirectToAction("Mural", "Home"); 
    } 
    else 
    { 
     ViewBag.Message = "Não foi possível completar o seu login via Facebook."; 
     return View("~/Views/Shared/Erro.cshtml"); 
    } 
} 

如果我把這些線,作爲示例所示:

[FacebookAuthorize(Permissions = ExtendedPermissions)] 

if (fbWebContext.IsAuthorized(ExtendedPermissions.Split(','))) 

我的應用程序停止工作。

任何人都有解決方案嗎? O另一個示例代碼獲得授權請求電子郵件和生日?

編輯

問題是這一行:

}, { scope: '@ViewBag.ExtendedPermissions' }); 

在我的應用程序應該是:

}, { scope: 'email,user_birthday,user_hometown' }); //alter to get permission to access user data 

我沒有設定一個範圍(燙髮)Facebook的訪問......但現在,我還有一個問題。

我的文化日期時間爲DD/MM/YYYY和Facebook的生日設置爲MM/DD/YYYY,我嘗試這樣做:

var birthdate = DateTime.Parse(me.birthday, CultureInfo.CreateSpecificCulture("pt-BR")); 

的Cuz我在巴西的文化,但我仍然得到的DateTime錯誤:

String was not recognized as a valid DateTime. 

任何人都知道如何解決這個問題?

+0

我有一個類似的解決方案...不知道這是否會解決..但嘗試offline_access,電子郵件,而不是電子郵件中的登錄按鈕。 – MoXplod

回答

1

這行做的一切工作正常與Facebook生日

var birthdate = DateTime.ParseExact(me.birthday, "MM/dd/yyyy", CultureInfo.InvariantCulture); 
相關問題