我正在尝试使用 c# MVC 在 Facebook 中获取页面数据。即使我授予 manage_pages 权限(范围),当我运行我的代码时它也不会显示任何页面。这是页面列表显示 0 的位置
string Accounts = "/me/accounts";
JSONObject pageData = api.Get(Accounts);
var data = pageData.Dictionary["data"];
List<JSONObject> pageList = data.Array.ToList<JSONObject>();
ViewBag.pageList = pageList;
以下是我尝试过的代码
public ActionResult returnfromfb()
{
string app_id = "AppID";
string app_secret = "AppSecret";
string scope = "manage_pages,publish_stream,status_update,user_about_me,user_hometown,user_location,email,offline_access";
string code = Request.QueryString["code"];
if (code == null)
{
Response.Redirect(string.Format("https://graph.facebook.com/oauth/authorize?client_id={0}&redirect_uri={1}&scope={2}",
app_id, Request.Url.AbsoluteUri, scope));
}
string AccessToken = "";
try
{
if (code != null)
{
string str = string.Format("https://graph.facebook.com/oauth/access_token?client_id={0}&redirect_uri={1}&scope={2}&code={3}&client_secret={4}",
app_id, Request.Url.AbsoluteUri, scope, Request["code"].ToString(), app_secret);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(str);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
byte[] Param = Request.BinaryRead(System.Web.HttpContext.Current.Request.ContentLength);
string strRequest = System.Text.Encoding.ASCII.GetString(Param);
req.ContentLength = strRequest.Length;
StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
streamOut.Write(strRequest);
streamOut.Close();
StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
string strResponse = streamIn.ReadToEnd();
if (strResponse.Contains("&expires"))
strResponse = strResponse.Substring(0, strResponse.IndexOf("&expires"));
AccessToken = strResponse.Replace("access_token=", "");
streamIn.Close();
}
Facebook.FacebookAPI api = new Facebook.FacebookAPI(AccessToken);
string requestEmail = "/me";
JSONObject fbemail = api.Get(requestEmail);
try
{
ViewBag.Email = fbemail.Dictionary["email"].String;
}
catch (Exception ex)
{
//errorLog.setError(ex, "LoginController.SaveFacebookData");
}
string Accounts = "/me/accounts";
JSONObject pageData = api.Get(Accounts);
var data = pageData.Dictionary["data"];
List<JSONObject> pageList = data.Array.ToList<JSONObject>();
ViewBag.pageList = pageList;
foreach (var page in pageList)
{
try
{
var id = page.Dictionary["id"].String;
string request = id;
JSONObject fbobject = api.Get(request);
try
{
ViewBag.BusinessName = fbobject.Dictionary["name"].String;
ViewBag.Address = fbobject.Dictionary["location"].ToDisplayableString();
ViewBag.PhoneNumber = fbobject.Dictionary["phone"].String;
}
catch (Exception ex)
{
//errorLog.setError(ex, "LoginController.SaveFacebookData");
}
}
catch (Exception ex)
{
//errorLog.setError(ex, "LoginController.SaveFacebookData");
}
}