我正在使用Spring Social获取解析令牌,并允许在该服务的用户页面上发布图像。但是我遇到了这个错误:
org.springframework.social.InsufficientPermissionException: Insufficient permission for this operation.
at org.springframework.social.facebook.api.impl.FacebookErrorHandler.handleFacebookError(FacebookErrorHandler.java:77)
at org.springframework.social.facebook.api.impl.FacebookErrorHandler.handleError(FacebookErrorHandler.java:59)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:700)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:653)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:628)
at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:398)
at org.springframework.social.facebook.api.impl.FacebookTemplate.publish(FacebookTemplate.java:348)
at org.springframework.social.facebook.api.impl.MediaTemplate.createAlbum(MediaTemplate.java:74)
at org.springframework.social.facebook.api.impl.MediaTemplate.createAlbum(MediaTemplate.java:67)
at br.com.igreja24h.service.FacebookService.createAlbum(FacebookService.java:157)
at br.com.igreja24h.service.FacebookService.postPhoto(FacebookService.java:180)
at br.com.igreja24h.service.FacebookService$$FastClassBySpringCGLIB$$6da0561b.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:85)
at br.com.igreja24h.aop.logging.LoggingAspect.logAround(LoggingAspect.java:85)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:629)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:618)
at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:62)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673)
at br.com.igreja24h.service.FacebookService$$EnhancerBySpringCGLIB$$d9313b23.postPhoto(<generated>)
at br.com.igreja24h.web.rest.AlbumResource.uploadAlbumPicture(AlbumResource.java:182)
at br.com.igreja24h.web.rest.AlbumResource$$FastClassBySpringCGLIB$$dcd05cb8.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:85)
at br.com.igreja24h.aop.logging.LoggingAspect.logAround(LoggingAspect.java:85)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
这是我获取权限令牌的方式:
public String createFacebookAuthorizationURL(String pageName, Long churchId) {
FacebookConnectionFactory connectionFactory = new FacebookConnectionFactory(facebookAppId, facebookSecret);
OAuth2Operations oauthOperations = connectionFactory.getOAuthOperations();
OAuth2Parameters params = new OAuth2Parameters();
params.setRedirectUri("https://" + this.hostName + "/api/facebook/" + churchId + "/response");
params.setScope("public_profile,pages_show_list,publish_pages,manage_pages,user_events");
return oauthOperations.buildAuthorizeUrl(params);
}
public String createFacebookAccessToken(String code, Long churchId) {
FacebookConnectionFactory connectionFactory = new FacebookConnectionFactory(facebookAppId, facebookSecret);
AccessGrant accessGrant = connectionFactory.getOAuthOperations().exchangeForAccess(code, "https://" + this.hostName + "/api/facebook/" + churchId + "/response", null);
return accessGrant.getAccessToken();
}
要使用以下方法将上传的图像发送到Facebook,
public String createAlbum(Album album) {
FacebookConnectionFactory connectionFactory = new FacebookConnectionFactory(facebookAppId, facebookSecret);
Church contentChurch = this.churchRepository.findOne(album.getChurch().getId());
String token = contentChurch.getFacebookTk();
String name = contentChurch.getFacebook()
.substring(contentChurch.getFacebook().lastIndexOf("/") + 1);
Facebook facebook = new FacebookTemplate(token, this.facebookAppId);
MediaOperations mediaOps = facebook.mediaOperations();
return mediaOps.createAlbum(album.getName(), album.getDescription());
}
public String postPhoto(Picture pic, File picFile) {
// @TODO finish this part
FacebookConnectionFactory connectionFactory = new FacebookConnectionFactory(facebookAppId, facebookSecret);
Church contentChurch = this.churchRepository.findOne(pic.getChurch().getId());
String token = contentChurch.getFacebookTk();
String name = contentChurch.getFacebook()
.substring(contentChurch.getFacebook().lastIndexOf("/") + 1);
Facebook facebook = new FacebookTemplate(token, this.facebookAppId);
MediaOperations mediaOps = facebook.mediaOperations();
log.debug("\n \n File Path ------> {} \n", picFile.getAbsolutePath());
Resource resource = null;
resource = new FileSystemResource(picFile.getAbsolutePath());
String albumID = createAlbum(pic.getAlbum());
if (resource.exists()) {
return mediaOps.postPhoto(albumID, resource, "POST PHOTO");
}
return "";
}
我搜索后发现,在'createFacebookAuthorizationURL'方法中请求的权限使我能够将图像发布到用户页面。发布或请求许可时我做错什么了吗?