【发布时间】:2015-07-09 20:18:06
【问题描述】:
大家好,我正在尝试实施 oauth 2.0 以通过 scribe 获取 facebook 访问令牌并不断获取空指针异常。下面的行不断得到一个空指针异常:
String authorizationUrl = oAuthService.getAuthorizaionUrl(Token.Empty())+"&"+STATE+"="+state;
下面是我的完整代码
@Controller
public class HomeController {
private static final String STATE = "state";
private String client_id = "********";
private String app_secret = "********";
private String url = "http://localhost:8080";
private static final Token EMPTY_TOKEN = null;
private OAuthService oAuthService;
private FacebookClient obj; //passing the token to restfb object
public HomeController() {}
public HomeController(OAuthService oAuthService) {
oAuthService = buildOAuthService(client_id, app_secret);
}
private OAuthService buildOAuthService(String client_id, String app_secret){
return new ServiceBuilder()
.provider(FacebookApi.class)
.apiKey(client_id)
.apiSecret(app_secret)
.callback(url + "/auth/facebook/callback")
.build();
}
@RequestMapping(value ="/login", method=RequestMethod.GET)
public String HomePage()
{
//home page and has the link to redirect to /auth/facebook
return "login";
}
//start the OAuth2 authorization flow
@RequestMapping(value="/auth/facebook")
public RedirectView startAuthentication(HttpSession httpSession) throws OAuthException {
String state = UUID.randomUUID().toString();
httpSession.setAttribute(STATE, state);
//Below is where i'm having a null pointer exception
String authorizationUrl = oAuthService.getAuthorizationUrl(Token.empty())+"&"+STATE+"="+state;
return new RedirectView(authorizationUrl);
}
@RequestMapping(value="/auth/facebook/callback")
public RedirectView callback(@RequestParam("code")String code, @RequestParam(STATE)String state, HttpSession httpsession) throws IOException
{
String stateFromSession = (String)httpsession.getAttribute(STATE);
if(!state.equals(stateFromSession)) {
return new RedirectView("/login");
}
Token accessToken = getAccessToken(code);
obj = new DefaultFacebookClient(accessToken.toString(), Version.VERSION_2_2);
return new RedirectView("/loggedin");
}
private Token getAccessToken(String code) {
Verifier verify = new Verifier(code);
return oAuthService.getAccessToken(Token.empty(), verify);
}
}
【问题讨论】:
标签: java facebook spring-mvc oauth