Facebook扩展的C#权限

时间:2012-06-13 16:25:14

标签: c# asp.net facebook permissions facebook-oauth

我正在开发一个Facebook应用程序(使用ASP.NET C#和Nathan Totten的Facebook C#SDK)并尝试访问扩展权限。 我试过localhost(在facebook上有一个现有的应用程序),每个人都在工作。但是当我尝试直接在Facebook上使用它时,我获得了一个空白页面(我仍然在Facebook上连接)。

这是我的代码:

Page / aspx:

protected void btn_ask_question_Click(object sender, EventArgs e)
    {
        if (ControllerAccess.testUserConnected())
        {
            if (txt_ask_question.InnerText != "")
            {
                Dictionary<string, object> action = new Dictionary<string,object>();
                action.Add("action", "askQuestion");
                action.Add("message", txt_ask_question.InnerText);
                action.Add("idTheme", ddl_choose_question_category.SelectedValue);
                HttpContext.Current.Session["ActionToDo"] = action;
                String s = Controller.GetExtendedPermission2(this, ControllerAccess.getScopeByAction("askQuestion"));
                try{
                    Response.Redirect(s, false);
                } catch(Exception ex)
                {
                }
            }
        }
    }

在我的Page_Load()中:

if (HttpContext.Current.Session["ActionToDo"] != null)
            {
                Dictionary<string, object> action = HttpContext.Current.Session["ActionToDo"] as Dictionary<string, object>;
                String s = action["action"].ToString();
                switch (s)
                {
                    case "askQuestion":
                        Guid idTheme;
                        Boolean themeExists =  Guid.TryParse(action["idTheme"].ToString(), out idTheme);
                        if(themeExists)
                        {
                            askQuestion(action["message"].ToString(), idTheme);
                            lbl_errors.Text = "Votre question a bien été posée";
                        }
                        break;
                    default:
                        break;
                }
                HttpContext.Current.Session["ActionToDo"] = null;
            }

在Control.cs中:

    public static string GetCustomOAuthDialogParameters(string AppID, string RedirectURI, string Scope, string State)
    {
        string CustomParameters = "?client_id=" + AppID + "&redirect_uri=" + RedirectURI + "&scope=" + Scope + "&state=" + State;
        return CustomParameters;
    }

    public static void GetExtendedPermission(Page page, String scope)
    {

        ecritureFichier("GetExtendedPermission");
        Label lbl_errors = page.Form.FindControl("lbl_errors") as Label;
        string OAuthDialogAbsoluteURL = "";
        try
        {
            string OAuthDialogURL = "https://www.facebook.com/dialog/oauth";
            string PageLocation = GetCurrentPageFacebookPublishedPath(page.Request); //The redirect page (eg: https://apps.facebook.com/democsharpsdk/TestsExtendedPermission.aspx)
            string UniqueReferenceCode = Guid.NewGuid().ToString();

            OAuthDialogAbsoluteURL = OAuthDialogURL + GetCustomOAuthDialogParameters(AppID, PageLocation, scope, UniqueReferenceCode);
            page.Response.Redirect(OAuthDialogAbsoluteURL, false);
        }
        catch (Exception exp)
        {
            lbl_errors.Text += "Erreur Catchée via ASP.NET : " + exp.Message;
        }
    }

使用此行时会出现空白页:page.Response.Redirect(OAuthDialogAbsoluteURL,false);

但是我记录了所有步骤,我的OAuthDialogAbsoluteURL是正确的:

https://www.facebook.com/dialog/oauth?client_id=XXXXXXXXXXXXXXXXX&redirect_uri=https://apps.facebook.com/name_of_my_app&scope=email&state=0443030f-dddd-4fe6-9d6c-9454409d01b3

当我手动将其键入地址栏时,一切正常。

您是否了解本地版本与已发布版本之间的区别或重定向无效的原因?也许facebook阻止了一些请求?

感谢。

2 个答案:

答案 0 :(得分:0)

如果要使用该应用测试不同的环境,请确保您的设置指向托管版本。 Facebook不允许您出于安全原因从未连接到您的应用的网站授权用户。

通常情况下,您会看到“网址不属于应用程序”或类似错误。

答案 1 :(得分:0)

我终于使用了Chrome调试器解决了我的问题。错误是:

Refused to display document because display forbidden by X-Frame-Options

我使用Javascript来修复它,并按照以下方式进行重定向:

Response.Write("<script>");
Response.Write("window.open('"+redirect+"','_top')");
Response.Write("</script>");

希望它能帮助一些人。