java - Get facebook access token with spring social FacebookConnectionFactory -
i trying implement facebook connection using spring social, based on example(from spring social manual):
facebookconnectionfactory connectionfactory = new facebookconnectionfactory("clientid", "clientsecret"); oauth2operations oauthoperations = connectionfactory.getoauthoperations(); oauth2parameters params = new oauth2parameters(); params.setredirecturi("https://my-callback-url"); string authorizeurl = oauthoperations.buildauthorizeurl(granttype.authorization_code, params); response.sendredirect(authorizeurl); // upon receiving callback provider: accessgrant accessgrant = oauthoperations.exchangeforaccess(authorizationcode, "https://my-callback-url", null); connection<facebook> connection = connectionfactory.createconnection(accessgrant);
my problem don't know redirect url should be.my code this:
@requestmapping("/face") public string communicate() { facebookconnectionfactory connectionfactory = new facebookconnectionfactory(clientid, clientsecret); oauth2operations oauthoperations = connectionfactory.getoauthoperations(); oauth2parameters params = new oauth2parameters(); //this remdirecturi should one? params.setredirecturi("http://dev01.spring:8080/spring/face"); string authorizeurl = oauthoperations.buildauthorizeurl(granttype.authorization_code, params); system.out.println(authorizeurl); //return "redirect:"+authorizeurl; // upon receiving callback provider: //accessgrant accessgrant = oauthoperations.exchangeforaccess(authorizationcode, "https://my-callback-url", null); //connection<facebook> connection = connectionfactory.createconnection(accessgrant); }
my authorizeurl this(from system.out line): https://graph.facebook.com/oauth/authorize?client_id=myid&response_type=code&redirect_uri=http%3a%2f%2fdev01.spring%3a8080%2fspring%2fface
if uncomment line continue oauth flow redirecting authorizeurl, i'm getting following error: webpage has redirect loop. question is, redirect uri should be.thank you.
very late edit, in hopes helps someone. controller , method whole oauth2 dance. must add worked when question asked, have no idea how behaves now.
@controller public class facebookcontroller { private static final string clientid = "clientidhere"; // clientid facebook app private static final string clientsecret = "clientsecret here"; // clientsecret private facebookconnectionfactory connectionfactory; // facebookconnectionfactory /* * if authorization given provider(code) token , bind api. */ @requestmapping("/facebook/callback") public string authorize(@requestparam("code") string authorizationcode,model model) { // exchange facebook code access token. accessgrant accessgrant = connectionfactory.getoauthoperations().exchangeforaccess(authorizationcode, "http://localhost:8080/testapp/facebook/callback", null); // not application deployed @ "http://localhost:8080/testapp" // connect facebook given token. connection<facebook> connection = connectionfactory.createconnection(accessgrant); // bind api facebook facebook = connection.getapi(); // user profile informations facebookprofile userprofile = facebook.useroperations().getuserprofile(); // @ point have acces facebook api. // ex can data user profile // create user facebook's user accounts details. user facebookuser = new user(userprofile.getfirstname(), userprofile.getlastname(), userprofile.getemail(), role.role_facebookuser, "socialuser"); return "redirect:/home"; } }
Comments
Post a Comment