我的网址如下......
http://localhost:1561/Facebook/Facebook/MainPage
控制器类
public class FacebookController : Controller
{
public ActionResult MainPage()
{
return View();
}
public PartialViewResult ShowMyDetails()
{
return PartialView("FacebookUserControl");
}
[CanvasAuthorize(Permissions = "user_about_me, publish_stream")]
public ActionResult GetMyDetails()
{
var fb = new FacebookClient();
dynamic CurrentUser = fb.Get("100003533388420");
User user = new User
{
id = CurrentUser.id,
first_name = CurrentUser.first_name,
gender = CurrentUser.gender,
last_name = CurrentUser.last_name,
name = CurrentUser.name,
username = CurrentUser.username
};
return Json
(
user,
JsonRequestBehavior.AllowGet
);
}
}
我的Web.Config中没有任何与Facebook相关的内容。
问题 - 如果我添加下面提到的行。我面临问题“ settings.AppId为null ”
[CanvasAuthorize(Permissions = "user_about_me, publish_stream")]
答案 0 :(得分:0)
让我先说明我之前没有使用过画布应用程序,所以不确定区别是什么。
但是,在创建FacebookClient时,您需要提供accessToken。
public ActionResult LoginFacebookAuth(string access_token)
{
var accessToken = access_token;
try
{
var facebookClient = new Facebook.FacebookClient(accessToken);
dynamic me = facebookClient.Get("me");
var fbUserId = me.id.ToString();
var email = me.email.ToString();
...
另外,在web.config中,我有一个条目。
<appSettings>
<add key="FbAppId" value="" />
传入access_token中的代码:
window.fbAsyncInit = function() {
FB.init({
appId: '{FbAppId from web.config}',
status: true, // check login status
cookie: true, // enable cookies to allow the server to access the session
xfbml: true // parse XFBML
});
};
$(document).ready(function() {
$('body').append('<div id="fb-root" name="fb-root"></div>');
$('.fbLoginLink') //When my fb icon is clicked, call FbLogin
.click(function(e) {
e.preventDefault();
FbLogin();
});
});
// Load the SDK Asynchronously
(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));
function FbLogin() {
FB.login(
function(response) {
if (response.authResponse) {
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
window.location = '@Url.RouteUrl("LoginFacebookAuth", new { })?accessToken=' + accessToken + '&uid=' + uid;
} else {
window.location = '/'; //User cancelled authorization
}
},
{ scope: 'email,user_birthday' }
); //FB.login
}
答案 1 :(得分:0)
public ActionResult GetFriendsDetails()
{
List<User> UserList = new List<Models.User>();
var fb = new FacebookClient("MyAppID", "MySecret");
//This will give the Access Token
dynamic FriendsList = fb.Get("MyAppID/friends");
foreach (var item in FriendsList.data)
{
dynamic CurrentUser = fb.Get(item.id);
UserList.Add
(
new User
{
id = CurrentUser.id,
first_name = CurrentUser.first_name,
gender = CurrentUser.gender,
last_name = CurrentUser.last_name,
name = CurrentUser.name,
username = CurrentUser.username
}
);
}
return Json
(
UserList,
JsonRequestBehavior.AllowGet
);
}