Here is quick and simple guide to setting up Foursquare Authentication using Go and retrieving your Check-In history.
p.s. At any time you can jump to the full sample code on Github
Let’s setup a basic Go Server
Now let’s setup a simple site to serve some HTML
The page, where we will click the login button from.
- The page handling redirection to Foursquare.
- The callback page handling information we get from Foursquare.
You will need the following dependencies
go get -u golang.org/x/…
Writing the application logic
Before starting remember to import the golang.org/x/oauth2 package.
To begin with, let’s write the main page handler:
Next you should register your app and we will need to create a variable we’ll use for storing data and communicating with Foursquare and the random state variable:
Handling communication with Foursquare
This is the code that creates a login link and redirects the user to it:
We use the foursquareOauthConfig variable to create a login link using the random state variable, and later redirect the user to it.
Now we need the logic that get’s the code after the user logs in and checks if the state variable matches:
Here is the full example:
Finally to get started run:
go run main.go
For further information about our API visit developer.foursquare.com