【问题标题】:Token is null when trying to grab facebook pic尝试抓取 Facebook pic 时令牌为空
【发布时间】:2015-07-20 00:54:48
【问题描述】:

我使用 scribe 从 facebook 获取 oauth 令牌并在 restfb 中传递令牌。我成功地做到了这一点。我能够检索有关我的信息,例如姓名、电子邮件和 ID。但是当我尝试检索照片时,我没有成功。我观察到当我尝试放置打印语句(调试目的)时它返回 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 或不同的令牌。拜托,我需要一些帮助来解决问题。

【问题讨论】:

  • 看起来您没有在登录期间要求访问用户照片的必要权限,您范围内的唯一权限是email
  • @CBroe 我添加了 user_photo 权限但仍然没有成功
  • 你有没有试过在graph api explorer上检查你的访问令牌和请求?

标签: java facebook oauth-2.0 scribe restfb


【解决方案1】:

我不确定这是否有用,但是在浏览网站和下载照片时,除了照片的 URL 之外,还必须存在三个标题键值,才能完成下载.这三个关键值是:

oh
oe
dl

完整的 URL GET 请求如下所示:

https://thefolder.net/photos/v/t1.09/thenumericcodeofthephoto.jpg?oh=somelongalphanumericcode&oe=somemorealphanumerics&dl=1

我的猜测是您缺少需要传递给服务器以检索文件的键值之一。您是否尝试过单独下载一张照片,而不是下载一个块中的所有文件?我会先尝试下载单个文件,因为它更容易排除故障,一旦成功,就尝试一次下载更多文件。

编辑***在查看了 restFB 的文档后,我想知道它不起作用的原因是否是因为 Facebook REST 命令不再受 Facebook 支持并且不起作用。我看到的所有 Facebook REST 访问都与图形有关。可能是您根本无法使用旧的 REST 接口访问 Facebook。

【讨论】:

  • @3m0I3y 我试过拍一张照片,但没有成功
  • 可能是不再支持 REST 命令。我一直在查看文档,但没有看到任何通过 REST 下载的有效命令。我希望你能找到你要找的东西。祝你好运。
  • 哦。是的。我找到了这个。看起来 REST 不再有效。 Facebook Developers Docs
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多