This repository has been archived by the owner on Oct 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
Authentication Example
John Cline edited this page Nov 23, 2013
·
1 revision
Some operation require that user has been authenticated with the Foursquare. Foursquare uses OAuth 2.0 to do that.
Note that you can save OAuthToken in database for the user and do not need to request it every time user accesses Foursquare.
In order to use FoursquareApi you need API credentials. You can get these credentials by registering your client to Foursquare at https://foursquare.com/oauth/.
public class AuthenticationExample {
public void authenticationRequest(HttpServletRequest request, HttpServletResponse response) {
FoursquareApi foursquareApi = new FoursquareApi("Client ID", "Client Secret", "Callback URL");
try {
// First we need to redirect our user to authentication page.
response.sendRedirect(foursquareApi.getAuthenticationUrl());
} catch (IOException e) {
// TODO: Error handling
}
}
public void handleCallback(HttpServletRequest request, HttpServletResponse response) {
// After user has logged in and confirmed that our program may access user's Foursquare account
// Foursquare redirects user back to callback url.
FoursquareApi foursquareApi = new FoursquareApi("Client ID", "Client Secret", "Callback URL");
// Callback url contains authorization code
String code = request.getParameter("code");
try {
// finally we need to authenticate that authorization code
foursquareApi.authenticateCode(code);
// ... and voilà we have a authenticated Foursquare client
} catch (FoursquareApiException e) {
// TODO: Error handling
}
}
}