Skip to main content

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.

"golang.org/x/oauth2" 
 This package will be used for oauth process. It will help in authnetication with facebook and return the token for further communication.

"github.com/huandu/facebook"
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


Process

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


Comments

Popular posts from this blog

Apache : setup basic auth with apache in windows

Authentication is any process by which you verify that someone is who they claim they are. Authorization is any process by which someone is allowed to be where they want to go or to have information that they want to have. I will show here how to set up basic auth on the apache with windows. Pre-requests  Windows VPS Apache server ( That's it ) ( In windows it might be difficult to setup the Apache alone. So instead use something ling xampp , wamp or laragon .) RestClient (  I personally use the postman , but you can use your preferable client)  Windows VPS provider Steps  Enable the necessary modules in the Apache Create the password file Set the auth directives in the virtual host file. Verify basic auth. Enable the  necessary   modules  in the Apache Open the httpd.conf file in the apache's conf folder. httpd.conf file Enable the necessary modules to make the basic auth working. Necessary modules  mod_auth_basic

Firebase - update a spacific fields of single element of object of array in firestore

Firebase - update a spacific fields of single element of object of array in firestore  Its actully advisable to use map instead of array when ever it is possible. But, there are cetain cases where you don't have option to do so.  For example, you are directly saving the response from some outer source without any modification and they send you an array. In this case you will have array to work with. Firestore does not support array here is why  "bad things can happen if you have multiple clients all trying to update or delete array elements at specific indexes. In the past, Cloud Firestore addressed these issues by limiting what you can do with arrays " For more details information you can refer to Kato Richardson post Best Practices: Arrays in Firebase .  Firestore document having array [ used from stackoverflow question ] Suppose you have array of object something like shown in array. Now you want to update endTime field of the object on the index [1]

Sciter : GUI Application with Golang using HTML/CSS

GUI library for golang sciter This is the words from Sciter's Web site, Sciter brings a stack of web technologies to desktop UI development. Web designers and developers can reuse their experience and expertise in creating modern looking desktop applications. Various GUI frameworks offer different UI declaration and styling languages, such as QML and  XAML (Microsoft WPF) . On the contrary, Sciter allows using time proven, robust, and flexible HTML and CSS for GUI definition and GPU accelerated rendering.   Before using sciter I already tried other alternatives but none of them was satisfactory as an example first i tried andlabs / ui  library   i already have written a post on it. You can read it on post gui programming with golang .  But this library is still under construction and has no support for production apps. Secondly, I go for electron but the problem was my simple calc like the app was of size 150mb. Which is 15mb of go and other was the e