Search code examples
facebookfacebook-graph-apifacebook-page

Post to Facebook Page as an App; Can this be done?


I'd like to know if the following is possible, as it is unclear to me after reading FB's documentation. Here's the scenario:

A Website has authors who write articles. The authors want to share links to these articles to FB Pages owned by influential persons ('influencers'). The influencers wish to allow the Website to automatically post article links to their Pages. Website authors are not FB users, so cannot be asked to login to FB. Instead article links needs to be shared via by the website's FB App. This will occur server side.

I know then that the Website's FB App needs permission to post to the influencer's Page, but I cannot request this permission client-side from Website authors because they are not FB users. It's reasonable that we could ask the influencers for a one-time setup to allow the FB App permission to post to their Pages, but I am concerned that Page access tokens seem to expire after a couple of hours, according to the documentation.

What I really want is the ability for an influencer to grant permanent access for the FB App to share links on their Page. Is this possible?

I'm not necessarily looking for code, just advice on what actions I need to perform where and when to get this set up. Thanks!


Solution

  • For reference if you're looking to extend a page access token:

    public async Task<string> ExtendAccessTokenAsync(string accessToken)
    {
        dynamic response = await ExtendAccessTokenAsync<dynamic>(accessToken);
    
        return response.access_token;
    }
    
    private async Task<T> ExtendAccessTokenAsync<T>(string accessToken)
    {
        var response = await httpClient.GetAsync($"oauth/access_token?grant_type=fb_exchange_token&client_id={options.AppId}&client_secret={options.AppSecret}&fb_exchange_token={accessToken}");
    
        if (!response.IsSuccessStatusCode)
        {
            return default(T);
        }
    
        var result = await response.Content.ReadAsStringAsync();
    
        return JsonConvert.DeserializeObject<T>(result);
    }
    

    T can be either a class to represent the returned object (see FB documentation for properties), or in my case I just use dynamic for T as I only want the access_token property.