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

Google blogger Ideas panel

Google blogger Ideas  I opened by blogger today, and..   I got this.  Google blogger Ideas  A panel suggesting a topic on which I can write my next blog. It's fetching unanswered question from web according to your previous post and topics. It was something, I was really looking for, after all it takes time to finding subject on which to write next and still being in the same niche.  Awesome feature Blogger! 

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]