Foursquare has revved their API, but there’s no Python Library to support it. Here’s how to use it from Python
- Get OAuth info from foursquare — the callback URL does not matter _but_ you must record here. https://foursquare.com/oauth/register. Fill in the results into
CLIENT_ID,CLIENT_SECRETandCALLBACK_URL - Run this, enter the URL in browser, get code as
FS_ACCESS_CODE, add back here - Run this, fill in
FS_ACCESS_TOKEN - Run this, you’re authorized: enjoy!
Notes
- Python OAuth2 from here: https://github.com/rodbegbie/python-oauth2
- This is entirely standing on the shoulder’s of Rod Begbie’s work.
Code:
import oauth2
import json
import pprint
OAUTH_BASE = "https://foursquare.com/oauth2/authenticate"
CLIENT_ID = ""
CLIENT_SECRET = ""
CALLBACK_URL = ""
FS_ACCESS_CODE = ""
FS_ACCESS_TOKEN = ""
client = oauth2.Client2(CLIENT_ID, CLIENT_SECRET, OAUTH_BASE)
if not CLIENT_ID:
print "=== Register your Foursquare data here"
print "=== Copy back CLIENT_ID, CLIENT_SECRET and CALLBACK_URL here"
print "https://foursquare.com/oauth/"
elif FS_ACCESS_TOKEN:
headers, content = client.request("https://api.foursquare.com/v2/users/self",
access_token = FS_ACCESS_TOKEN)
pprint.pprint({
"headers" : headers,
"content" : json.loads(content),
})
elif not FS_ACCESS_CODE:
print "=== Enter this URL in your browser."
print "=== Copy the CODE in the URL that results into FS_ACCESS_CODE"
print client.authorization_url(redirect_uri = CALLBACK_URL,
endpoint='authenticate')
else:
print "=== Here is your Access Token"
print "=== Copy into FS_ACCESS_TOKEN as start using Foursquare"
print client.access_token(FS_ACCESS_CODE, redirect_uri= CALLBACK_URL,
grant_type='authorization_code')["access_token"]