The Foursquare docs walk through the process in great detail. There are 2 options:
- Web Applications Code Flow
- Web Applications Token Flow
Both these options will require you to setup an app through the Foursquare Developer site. You'll need to setup a redirect URL for Foursquare to redirect back to. This is usually a publically accessible URL, but a locahost
URL also works for testing purposes.
The first, the Code Flow, follows a standard OAuth process:
Direct users (generally done through a link or button) to
https://foursquare.com/oauth2/authenticate?client_id=YOUR_CLIENT_ID&response_type=code&redirect_uri=YOUR_REGISTERED_REDIRECT_URI`
If the user accepts, they will be redirected back to
https://YOUR_REGISTERED_REDIRECT_URI/?code=CODE
Your server should exchange the code it got in step 2 for an access token. Make a request for
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
The response will be JSON
{ access_token: ACCESS_TOKEN }
This access token is what you're looking for.
The second method, the token flow is slightly easier:
Redirect users who wish to authenticate to
https://foursquare.com/oauth2/authenticate?client_id=CLIENT_ID&response_type=token&redirect_uri=YOUR_REGISTERED_REDIRECT_URI
If a user accepts, they will be redirected back to
https://YOUR_REGISTERED_REDIRECT_URI/#access_token=ACCESS_TOKEN
This access_token
query param is what you're looking for.