golang oauth2 facebook example

Todays most sites are integrated with social platforms like twitter, facebook, google plus for one or other purpose. I am writing this post to show how you can integrate Facebook with your golang app using facebook oauth2.

Just Some Word About OAuth2

OAuth 2 is an authorization framework that enables applications to obtain limited access to user accounts on an HTTP service, such as Facebook, GitHub, and DigitalOcean.
For More information read this article ReadMore

Create App For Facebook

To integrate Facebook with your golang app, first of all, you have to need a facebook app_id and app_secret. Those are app identifiers for facebook. 

To create an app on facebook you should have an account on Facebook ( and ( think you might have already ). Using your Facebook account login on developer.facebook.com 

developer facebook  portal
developer facebook portal

 After login click on MyApps dropdown and click on Add a New App Button

create a new facebook app
Create a new Facebook app 

Create New AppId by providing the Display name and contact email address.
Create a new app id 

After creation of your new app, you will see the dashboard of that application

Click on the add products 
Facebook app dashboard

After clicking on Add product, you will be able to see the list of the product as shown in the image. Because we want to support Oauth2 using the Facebook we have to Setup Facebook Login. So hover over first product facebook login and click on Set up.
Set Up Facebook Login

Instead of Quickstart go and click on the settings as shown in image ( because I find this way easy and useful than quickstart  way)

Open settings of facebook login

Add  the valid OAuth redircets url
Add valid callback urls 

Connect Golang App with facebook app

Write GoLang Code to connect your golang app with the app you created on the facebook using the oAuth2.

For this we will use to packages.

 This package will be used for oauth process. It will help in authnetication with facebook and return the token for further communication.

After getting token you can make further request to the facebook using that token. For that we will use this package. It is wrapper on facebook api.

Add facebook oauth2 sample

I am assuimg that you know how to create a web app in golang. So i am just showing the code snippet that handles the oauth2 request for facebook


fbHandler function will redirect you to facebook login page for your app with appropriate request payload (that is the scopes/permissions you are requesting ) and after complition of the authentication from the facebook it will redirect you to your callback url you have set in the facebook login settings page ( and it should be same as the RedirectURL filed of the oauth2.Config  ) with the appropriate response ( that is either a token or error )

Now though this is preety streight forward. You might get errors like 191 and 100 most probebly.

Errors i get and solutions i found

facebook oauth error 191
(Url can not load )
This is the error when RedirectURL does not match with the one you add in valid oauth callback urls.So match those two.

facebook oauth error 100
( verification code not match)
I get this error when i was using this
token, err := conf.Exchange(oauth2.NoContext, "code")
You can see this full code of this at here See Code
Don't know why. But then instead of using the Exchage i used the normal Oauth and error gone.

I encourge you to share more errors ( with solution in comments ).
It will be more usefull