Skip to content

Instantly share code, notes, and snippets.

@dlo
Last active February 27, 2020 12:27
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save dlo/7177249 to your computer and use it in GitHub Desktop.
Save dlo/7177249 to your computer and use it in GitHub Desktop.
Download all your Foursquare checkins with Python.
# pip install requests
import requests
import json
url_template = 'https://api.foursquare.com/v2/users/self/checkins?limit=250&oauth_token={}&v=20131026&offset={}'
# If you navigate to https://developer.foursquare.com/docs/explore, Foursquare
# will generate an OAuth token for you automatically. Cut and paste that token
# below.
token = ""
offset = 0
data = []
with open("/tmp/checkins.json", 'w') as f:
while True:
response = requests.get(url_template.format(oauth_token, offset))
if len(response.json()['response']['checkins']['items']) == 0:
break
data.append(response.json())
offset += 250
f.write(json.dumps(data))
@rootulp
Copy link

rootulp commented Dec 28, 2016

Hi @dlo - I made a few changes on a fork. Hoping they might be useful to merge here.

  • Replaced token with oauth_token because the latter is actually used in the script
  • Save foursquare_checkins.json to the local directory instead of /tmp/checkins.json in hopes of making this more intuitive
  • Added instructional comments to the top
  • Split the JSON into multiple lines so we get a readable JSON file rather than one long line

@yaylinda
Copy link

yaylinda commented Sep 2, 2017

I believe there is an error on line 13. You have not defined the variable oauth_token, I think you mean token, which you defined on line 10. Thanks for the script though!

@skywinder
Copy link

As I can see, link developer.foursquare.com/docs/explore doesn't work anymore (got 404).

Is any updates, how to process rrequest with new API?
https://foursquare.com/developers/apps/

There is option to get app id and secret.

@abravitov
Copy link

abravitov commented Feb 23, 2020

After you create an app, you must fill the "REDIRECT URL" in the app settings (arbitrary).

Then you should open the following link in the browser:

"https://foursquare.com/oauth2/authenticate?client_id=YOUR_CLIENT_ID&response_type=code&redirect_uri=YOUR_REGISTERED_REDIRECT_URI" (replace YOUR_CLIENT_ID and YOUR_REGISTERED_REDIRECT_URI with app id and redirect url).

After that, you are redirected to the auth page. Allow the app to use your data, then you will be redirected to

"https://YOUR_REGISTERED_REDIRECT_URI/?code=CODE". The code is needed for obtaining the auth token.

You add

r= 'https://foursquare.com/oauth2/access_token?client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET&grant_type=authorization_code&redirect_uri=YOUR_REGISTERED_REDIRECT_URI&code=CODE'

req = requests.get(r)
req.json()

This will print out the oauth code for the script above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment