Created
April 5, 2012 18:33
-
-
Save philihp/2313113 to your computer and use it in GitHub Desktop.
Facebook OAuth with Scribe in Struts 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.struts.action.*; | |
import com.google.gson.*; | |
import org.scribe.builder.*; | |
import org.scribe.builder.api.*; | |
import org.scribe.model.*; | |
import org.scribe.oauth.*; | |
public class Authenticate extends Action { | |
public ActionForward execute(ActionMapping mapping, ActionForm form, | |
HttpServletRequest request, HttpServletResponse response) | |
throws Exception { | |
OAuthService service = new ServiceBuilder() | |
.provider(FacebookApi.class) | |
.apiKey(Facebook.client_id) | |
.apiSecret(Facebook.client_secret) | |
.callback(Facebook.redirect_uri) | |
.build(); | |
String verifierCode = request.getParameter("code"); | |
if(verifierCode == null) { | |
String authURL = service.getAuthorizationUrl(null); | |
return new ActionForward(authURL, true); | |
} | |
else { | |
Token accessToken = service.getAccessToken((Token)null, new Verifier(verifierCode)); | |
OAuthRequest authRequest = new OAuthRequest(Verb.GET, "https://graph.facebook.com/me"); | |
service.signRequest(accessToken, authRequest); | |
Response authResponse = authRequest.send(); | |
JsonParser parser = new JsonParser(); | |
JsonObject authData = parser.parse(authResponse.getBody()).getAsJsonObject(); | |
request.setAttribute("facebookId",authData.getAsJsonPrimitive("id").getAsString()); | |
return mapping.findForward(...); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
More details at http://www.philihp.com/blog/2012/authenticating-to-facebook-in-struts-1-with-scribe-and-gson/