Laravel 5.6 : Posting to Profiles and Pages with Facebook PHP SDK

Let’s get started by creating a new Laravel project by running

composer create project laravel/laravel acme

Add laravel/socialite and facebook/graph-sdk to composer.json file

and run composer install to install them. We are using socialite package to authenticate users with Facebook OAuth service. After authentication, we’ll be storing user access token in the database and use it to make calls to OAuth service on the user’s behalf. I wrote a very detailed tutorial on social authentication with socialite. Follow the steps below to integrate Facebook SDK in your laravel application and using it to post to profile and pages.

Step 1: Create a Facebook App.

Visit facebook developers console and create a new app. Fill in details and create App ID. We’ll be using this app to authenticate users and post on their behalf. Go to Settings > Basic and copy app id and secret. Add a new product and choose Facebook login.

Step 2: Populate Facebook Credentials

Edit config/service.php  file. Add following lines at the end of the services array.

Edit your project’s  .env  file.

Step 3: Add Redirect URL to App Valid OAuth Redirect URIs

Go to your App > Products > Facebook Login > Settings and update Valid OAuth Redirect URIs.  For apps created after March 2018, it’s compulsory to use HTTPS to use OAuth service. Even if you’re requesting from your local dev environment, you must have https enabled to make use of their service. You can’t disable Enforce HTTPS options in your app settings. If you’re on Windows platform, you can easily configure XAMPP to use HTTPS. If you’re running Linux, you can use a self-signed certificate with nginix or apache. After settings up your self-signed certs, you can enable allow-insecure-localhost  flag in chrome://flags settings to allow requests to localhost over HTTPS even when an invalid certificate is presented. 

Step4: Integrating Facebook

We are using Laravel service providers to bootstrap Facebook to avoid unnecessary instantiation of Facebook object every time we’ll be making calls to Facebook OAuth service. Create a new FacebookServiceProvider.

php artisan make:provider FacebookServiceProvider

Now edit app/Providers/FacebookServiceProviders.php and add following code.

In register()  method of our service provider, we are defining an implementation of Facebook\Facebook in the service container. We are using Laravel’s built-in config()  method to retrieve Facebook OAuth credentials from config/services.php  file and using it to construct Facebook class object which we will be using throughout our application to make calls to Facebook API.

Step 4: Database Migrations

After populating database credentials in your project’s  .env  file. Modify and run migrations to create database tables nessary to store our OAuth user. This is what our User migration looks like.

Step 5: Setting up authentication routes and controllers

Run php artisan make:auth  to generate Laravel’s default authentication scaffolding. Edit routes/web.php  file to add social authentication routes.

Edit app/Http/Controllers/Auth/LoginController.php

In redirectToFacebookProvider()  method, we are requesting publish and manage permissions for profile and pages by using Socialite scopes  method. App user will be redirected to Facebook OAuth page after visiting  /login/facebook route. Upon accepting they will be redirected back to login/facebook/callback route and we will retrieve their access token and profile details to create a new user or update existing one with a fresh token. Now we’ll be using this token to make calls to Graph API on the behalkf of user.

Retrieving User Profile via the Graph API

Let’s create a new controller to handle requests for using Graph API.

php artisan make:controller GraphController

Add an authenticated route to  routes/web.php

Now define retrieveUserProfile method in  GraphController

Posting to Profiles

Add a new method publishToProfile to GraphController for handling requests to create profile posts.

Add a corresponding route to routes/web.php  call it.

To confirm if your post was created, you can check id on the response object. If the request was successful it will be populated with the id of the newly created node. In case of an unsuccessful request, it will be null.

Posting to Pages

Method of posting to pages is different from profiles. When you’re posting to your profile, you’re using your own access token to make the request. Every page your manage has its own access token and you have to retrieve it before making any requests. Add a new method getPageAccessToken to GraphController

We’re passing $page_id to getPageAccessToken to retrieve access token. You can get your page id from about section of your facebook page. After retrieving all pages, we are returning access token.  Now we’ll use this token to post to a page. Add publishToPage method to GraphController  and route to call it.

Creating Photo Posts

To create a media post, all you have to do is to pass source  along with text message. It should be an instance of FacebookFile entity. Calling fileToUpload('/path/to/file.jpg') with absolute image path also return an instance of FacebookFile.

I hope after following above steps, you’ll be able to easily integrate Facebook SDK into your Laravel app. I’ve also created an example project repository on github. However, If you’re still facing any issue, please comment and I’ll try to help you solve your problem.

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *