我使用scribe从facebook获取oauth令牌并在restfb中传递令牌。我成功地实现了这一目标。我能够检索关于我的信息,例如姓名,电子邮件和身份证。但是当我尝试检索照片时,我没有成功。我发现当我尝试使用print语句(调试目的)时它返回null,因为我无法检索有关照片的任何信息。是令牌的问题,因为我在尝试保存到mongodb时得到一个空指针。我也试过打印令牌但是null?需要一些帮助。
以下是我的代码:
@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 ObjectMapper objectMapper;
private OAuthService oAuthService;
private FacebookClient facebookClient;
@Autowired
UserAccountService userAccountService;
@Autowired
UserPhotoService userPhotoService;
public HomeController() {
this.oAuthService = buildOAuthService(client_id, app_secret);
}
//starts the oauth by passing necessary parameters and initializes oauthservice.
private OAuthService buildOAuthService(String client_id, String app_secret){
return new ServiceBuilder()
.apiKey(client_id)
.apiSecret(app_secret)
.callback(url+"/auth/facebook/callback") //redirects the callback and must match the url in facebook settings.
.provider(FacebookApi.class)
.scope("email")
.build();
}
//login page redirects to facebook to get user details
@RequestMapping(value="/")
public String HomePage() {
return "login";
}
//link redirects to facebook for access token.
@RequestMapping(value="/auth/facebook", method=RequestMethod.GET)
public RedirectView startAuthentication(HttpSession httpSession) throws OAuthException {
String state = UUID.randomUUID().toString();
httpSession.setAttribute(STATE, state);
String authorizationUrl = oAuthService.getAuthorizationUrl(Token.empty())+"&"+STATE+"="+state;
return new RedirectView(authorizationUrl);
}
//method handles the callback from facebook
@RequestMapping(value="/auth/facebook/callback")
public String callback(@RequestParam("code")String code, @RequestParam(STATE)String state, HttpSession httpsession) throws IOException
{
//checks state parameter.
String stateFromSession = (String)httpsession.getAttribute(STATE);
httpsession.removeAttribute(STATE);
if(!state.equals(stateFromSession)) { //incase of failure redirects user to login
return "login";
}
//Exchanges the access token
Token accessToken = getAccessToken(code);
//pass token to restfb
this.facebookClient = new DefaultFacebookClient(accessToken.getToken(), Version.VERSION_2_2);
return "logged"; //successfully logged in.
}
//retrieves the access token
private Token getAccessToken(String code) {
Verifier verify = new Verifier(code);
return oAuthService.getAccessToken(Token.empty(), verify);//Token.Empty() method in scribe and handles OAuthservice for both OAuth1 and 2.
}
@RequestMapping(value="/{user-id}/attending", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
public @ResponseBody void getProfile(@PathVariable("user-id")String userId) {
User me = facebookClient.fetchObject("me", User.class);
userAccountService.create(me);//able to retrieve user info successfully
userPhotoService.create(me, facebookClient); // i pass the token
return;
}
下面的课程是检索照片的服务:
@Repository
public class UserPhotoServiceImpl implements UserPhotoService {
@Autowired
private PhotoRepo photorepo; //extends mongorepository
public UserPhotos create(User me, FacebookClient faceboookClient){
UserPhotos userPhotos = new UserPhotos();
Connection<Photo> photoConnection = facebookClient.fetchConnection("me/photos", Photo.class);
for(List<Photo> eachPhoto: photoConnection) {
for(Photo p: eachPhoto){
userPhotos.setPhotoId(p.getId());//storing info in userPhotos class
userPhotos.setCountry(p.getPlace().getLocation().getCountry());
}
}
System.out.println(userPhotos.getCountry());//for debugging purposes returns null and cannot retrieve any photos.
photorepo.save(userPhotos); //trying to save to mongoDB but returns null.
return userPhotos;
}
} //
以下是我的UserPhotoService类:
public interface UserPhotoService {
UserPhotos create(User me, FacebookClient facebookClient);
}
似乎令牌返回null或不是相同的令牌。拜托,我需要一些关于错误的帮助。