java令牌在尝试获取facebook pic时为空
我使用scribe从facebook获取oauth令牌,并在restfb中传递该令牌。我成功地做到了这一点。我可以检索我的信息,如姓名、电子邮件和id。但当我尝试检索照片时,我没有成功。我注意到,当我尝试放置print语句(调试目的)时,它返回null,因为我无法检索有关照片的任何信息。是令牌的问题,因为我在尝试保存到mongodb时得到一个空指针。我还试着打印令牌,但为空?我需要一些帮助
下面是我的代码:
@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或不是相同的令牌。求你了,我需要你帮我解决问题
# 1 楼答案
我不确定这是否有用,但在浏览网站和下载照片时,除了照片的URL外,还必须提供三个标题键值,才能完成下载。这三个关键价值是:
完整的URL GET请求如下所示:
我的猜测是,您缺少一个需要传递给服务器以检索文件的键值。你有没有试过自己下载一张照片,而不是一块下载所有的文件?我会尝试先下载一个单一的文件,因为它更容易排除故障,一旦成功,我会尝试一次下载更多的文件
编辑***看了restFB的文档后,我想知道它不起作用的原因是否是因为Facebook不再支持Facebook的REST命令,也不起作用。我看到的所有Facebook REST访问都与图形有关。可能是因为您无法使用旧的REST界面访问Facebook