Skip to content

Instantly share code, notes, and snippets.

@philihp
Created April 5, 2012 18:33
Show Gist options
  • Save philihp/2313113 to your computer and use it in GitHub Desktop.
Save philihp/2313113 to your computer and use it in GitHub Desktop.
Facebook OAuth with Scribe in Struts 1
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(...);
}
}
}
@philihp
Copy link
Author

philihp commented Apr 6, 2012

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