Foursquare Intersections logo
Explore
Subscribe
Engineering

#GoLang Support for OAuth

Written by Foursquare API on Nov 08, 2016 - Read time: 2 min - Read Later

Gopher designed by Renee French

(Gopher designed by Renee French)

We recently made some changes to the sub-package OAuth inside the programming language Go, this makes it even easier to start building things with Foursquare.

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

Getting Started

Let's setup a basic Go Server

<iframe frameborder="0" height="0" scrolling="no" src="" width="0">https://medium.com/media/f0164158abcfe74cc57f9373bacecce8/href</iframe>

Now let's setup a simple site to serve some HTML

<iframe frameborder="0" height="0" scrolling="no" src="" width="0">https://medium.com/media/66e895113509b06b6e3c8c31a803f9dd/href</iframe>

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.

<iframe frameborder="0" height="0" scrolling="no" src="" width="0">https://medium.com/media/fddaaf94ac94719dcff64d3c0e64098e/href</iframe>

Dependencies

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:

<iframe frameborder="0" height="0" scrolling="no" src="" width="0">https://medium.com/media/3717f4ac991c84819997363809913340/href</iframe>

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:

<iframe frameborder="0" height="0" scrolling="no" src="" width="0">https://medium.com/media/46eb455a573eeb6d9955efe8f9d16155/href</iframe>

Handling communication with Foursquare

This is the code that creates a login link and redirects the user to it:

<iframe frameborder="0" height="0" scrolling="no" src="" width="0">https://medium.com/media/cddd395bf88bd0b1f8bee33618db986a/href</iframe>

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:

<iframe frameborder="0" height="0" scrolling="no" src="" width="0">https://medium.com/media/806d665e63eec52d49bf9dbfae385bd9/href</iframe>

Here is the full example:

<iframe frameborder="0" height="0" scrolling="no" src="" width="0">https://medium.com/media/e8e9e476545a4904b1d3e84bfe020434/href</iframe>

Finally to get started run:

go run main.go

For further information about our API visit developer.foursquare.com

Subscribe

Follow Foursquare API

#GoLang Support for OAuth

Read Later

Pardot response heading