Manbir
Manbir

Reputation: 5

Share Post as Facebook App on Facebook page

I have been using facebook api to share post on facebook page as facebook app. I have created facebook app. The post also gets shared but the problem is, it is not being posted as a facebook app. Instead it asks user for login and then it shares post as a user. Any help regarding how could I share a post as a Facebook App would be appreciated as I have been trying this for a long time. Thanx in advance friends. The code that I have been using is as follows.

using Facebook;

  protected void CheckAuthorization()
    {
        string authorizationCode = Request.QueryString["code"];
        string access_token = Facebook_GetAccessToken(authorizationCode);
        FacebookShare(access_token);
    }

     private string Facebook_GetAccessToken(string pAuthorizationCode)
    {
        string urlGetAccessToken = "https://graph.facebook.com/oauth/access_token";
        urlGetAccessToken += "?client_id=my app id";
        urlGetAccessToken += "&client_secret=app secret";
        urlGetAccessToken += "&redirect_uri=" + Facebook_GetRedirectUri();
        urlGetAccessToken += "&code=" + pAuthorizationCode;

        string responseData = RequestResponse(urlGetAccessToken);
        if (responseData == "")
        {
            return "";
        }
        NameValueCollection qs = HttpUtility.ParseQueryString(responseData);
        string access_token = qs["access_token"] == null ? "" : qs["access_token"];

        return access_token;
    }

    protected void FacebookShare(string token)
    {
        if (token != null)
        {
            Int64 transactionid = Convert.ToInt64(Cache["transactionid"]);
            string app_id = "my app id";

            string app_secret = "app secret";

            string scope = "offline_access,read_stream,publish_actions,publish_stream,manage_pages,status_update";
            dynamic parameters = new ExpandoObject();

            parameters.link = "my website link";
            parameters.name = "my project name";
            parameters.picture= "my website logo";
            var client = new FacebookClient(token);
            client.Post("/1374775846180409/feed", parameters); // this is my facebook page id where I want to share post.
            Response.Cookies["transaction"].Expires = DateTime.Now;
        }
        else
        {
        }
    }

Regarding above code, I am getting Facebook Authorization code successfully.

Upvotes: 0

Views: 221

Answers (1)

asarva
asarva

Reputation: 81

You can only ask your user who has logged into your app to post on facebook, apps as such are not allowed to post. You will need a page access token to post as the page, which have permissions to modify the data belonging to a Facebook Page. To obtain a page access token you need to start by obtaining a user access token, for which your user has to be logged into your app and asking for the manage_pages permission scope. Next, you can GET /me/accounts, which will return a page access token for each page that you manage.

This can not be accomplished using offline access or without logging in.

Upvotes: 5

Related Questions