I want to use Spring with Flickr, but when I add a photo I receive this error "2023-12-16T12:53:58.182+01:00 ERROR 616 --- [nio-8080-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: com.flickr4java.flickr.FlickrException: 100: Invalid API Key (Key has invalid format)] with root cause
com.flickr4java.flickr.FlickrException: 100: Invalid API Key (Key has invalid format) at com.flickr4java.flickr.uploader.Uploader.sendUploadRequest(Uploader.java:156) ~[flickr4java-3.0.8.jar:na] at com.flickr4java.flickr.uploader.Uploader.upload(Uploader.java:107) ~[flickr4java-3.0.8.jar:na] at project.sgs.Service.FlickrService.savePhoto(FlickrService.java:24) ~[classes/:na] at project.sgs.Strategy.SaveArticlePhoto.savePhoto(SaveArticlePhoto.java:24) ~[classes/:na] at project.sgs.Strategy.SaveArticlePhoto.savePhoto(SaveArticlePhoto.java:13) ~[classes/:na] at project.sgs.Strategy.StrategyPhotoContext.SavePhoto(StrategyPhotoContext.java:27) ~[classes/:na] at project.sgs.Controller.PhotoController.save(PhotoController.java:22) ~[classes/:na] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na] at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na] at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na] at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:207) ~[spring-web-6.0.6.jar:6.0.6] at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:152) ~[spring-web-6.0.6.jar:6.0.6] at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) ~[spring-webmvc-6.0.6.jar:6.0.6] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:884) ~[spring-webmvc-6.0.6.jar:6.0.6] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797) ~[spring-webmvc-6.0.6.jar:6.0.6] at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-6.0.6.jar:6.0.6] at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1081) ~[spring-webmvc-6.0.6.jar:6.0.6] at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:974) ~[spring-webmvc-6.0.6.jar:6.0.6] at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1011) ~[spring-webmvc-6.0.6.jar:6.0.6] at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:914) ~[spring-webmvc-6.0.6.jar:6.0.6]"
package project.sgs.Config;
import com.flickr4java.flickr.Flickr;
import com.flickr4java.flickr.FlickrException;
import com.flickr4java.flickr.REST;
import com.flickr4java.flickr.RequestContext;
import com.flickr4java.flickr.auth.Auth;
import com.flickr4java.flickr.auth.Permission;
import com.github.scribejava.apis.FlickrApi;
import com.github.scribejava.core.builder.ServiceBuilder;
import com.github.scribejava.core.model.OAuth1AccessToken;
import com.github.scribejava.core.model.OAuth1RequestToken;
import com.github.scribejava.core.oauth.OAuth10aService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.ExecutionException;
@Configuration
@Slf4j
public class FlickrConfiguration {
@Value("${flickr.apiKey}")
private String apiKey;
@Value("${flickr.apiSecret}")
private String apisecret;
@Value("${flickr.appKey}")
private String appKey;
@Value("${flickr.appSecret}")
private String appsecret;
private Flickr flickr;
/* @Bean
public Flickr getFlickr() throws IOException, ExecutionException, InterruptedException, FlickrException {
Flickr flickr=new Flickr(apiKey,apisecret,new REST());
OAuth10aService oAuth10aService=new ServiceBuilder(apiKey)
.apiSecret(apisecret)
.build(FlickrApi.instance(FlickrApi.FlickrPerm.DELETE));
final Scanner scanner=new Scanner(System.in);
final OAuth1RequestToken oAuth1RequestToken=oAuth10aService.getRequestToken();
final String authUrl=oAuth10aService.getAuthorizationUrl(oAuth1RequestToken);
System.out.println(authUrl);
System.out.println("paste it here >>");
final String authVerifier=scanner.nextLine();
OAuth1AccessToken oAuth1AccessToken=oAuth10aService.getAccessToken(oAuth1RequestToken,authVerifier);
System.out.println(oAuth1AccessToken.getToken());
System.out.println(oAuth1AccessToken.getTokenSecret());
Auth auth=flickr.getAuthInterface().checkToken(oAuth1AccessToken);
System.out.println("----------------------------");
System.out.println(auth.getToken());
System.out.println(auth.getTokenSecret());
return flickr;
}*/
@Bean
public Flickr getFlickr(){
flickr = new Flickr(apiKey, apisecret, new REST());
Auth auth = new Auth();
auth.setPermission(Permission.DELETE);
auth.setToken(appKey);
auth.setTokenSecret(appsecret);
RequestContext requestContext = RequestContext.getRequestContext();
requestContext.setAuth(auth);
flickr.setAuth(auth);
return flickr;
}
}
package project.sgs.Controller;
import com.flickr4java.flickr.FlickrException;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import project.sgs.Exeption.InvalidOperationExeption;
import project.sgs.Strategy.StrategyPhotoContext;
import java.io.IOException;
@RestController
@RequiredArgsConstructor
@RequestMapping("/photo")
public class PhotoController {
@Autowired
private StrategyPhotoContext strategyPhotoContext;
@PostMapping("/save/{id}/{title}/{context}")
public ResponseEntity<Object> save(@PathVariable Long id, @PathVariable String title, @PathVariable String context, @RequestPart MultipartFile photo) throws IOException, FlickrException, InvalidOperationExeption {
Object objet=strategyPhotoContext.SavePhoto(context,id,photo.getInputStream(),title);
return new ResponseEntity<>(objet, HttpStatus.CREATED);
}
}
package project.sgs.Service;
import com.flickr4java.flickr.Flickr;
import com.flickr4java.flickr.FlickrException;
import com.flickr4java.flickr.uploader.UploadMetaData;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import java.io.InputStream;
@Service
@Slf4j
@RequiredArgsConstructor
public class FlickrService {
@Autowired
private Flickr flickr;
public String savePhoto(InputStream photo,String title) throws FlickrException {
UploadMetaData uploadMetaData=new UploadMetaData();
uploadMetaData.setTitle(title);
String photoid=flickr.getUploader().upload(photo,uploadMetaData);
return flickr.getPhotosInterface().getPhoto(photoid).getMedium640Url();
}
}
package project.sgs.Strategy;
import com.flickr4java.flickr.FlickrException;
import lombok.Setter;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import project.sgs.Exeption.ErrorCode;
import project.sgs.Exeption.InvalidOperationExeption;
import java.io.InputStream;
@Service
public class StrategyPhotoContext {
private BeanFactory beanFactory;
private Strategy strategy;
@Setter
private String context;
@Autowired
public StrategyPhotoContext(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
public Object SavePhoto(String context, Long id, InputStream photo,String title) throws InvalidOperationExeption, FlickrException {
determinContext(context);
return strategy.savePhoto(id,photo,title);
}
private void determinContext(String context) throws InvalidOperationExeption {
final String beanName=context+"Strategy";
switch (context){
case "article":
strategy=beanFactory.getBean(beanName,SaveArticlePhoto.class);
break;
case "client":
strategy=beanFactory.getBean(beanName,SaveClientPhoto.class);
break;
case "fournisseure":
strategy=beanFactory.getBean(beanName,SaveFournisseurePhoto.class);
break;
case "utilisateure":
strategy=beanFactory.getBean(beanName,SaveUtilisateurePhoto.class);
break;
case "entreprise":
strategy=beanFactory.getBean(beanName,SaveEntreprisePhoto.class);
break;
default:throw new InvalidOperationExeption("conntext inconu", ErrorCode.Context_NOT_FOUND);
}
}
}
package project.sgs.Strategy;
import com.flickr4java.flickr.FlickrException;
import java.io.InputStream;
public interface Strategy <T>{
T savePhoto(Long id,InputStream photo,String titre) throws FlickrException;
}
package project.sgs.Strategy;
import com.flickr4java.flickr.FlickrException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import project.sgs.Dto.ArticleDto;
import project.sgs.Service.ArticelService;
import project.sgs.Service.FlickrService;
import java.io.InputStream;
@Service("articleStrategy")
@Slf4j
@RequiredArgsConstructor
public class SaveArticlePhoto implements Strategy<ArticleDto>{
@Autowired
private FlickrService flickrService;
@Autowired
private ArticelService articelService;
@Override
public ArticleDto savePhoto(Long id, InputStream photo, String titre) throws FlickrException {
ArticleDto article=articelService.getArticleById(id);
String UrlPhoto=flickrService.savePhoto(photo,titre);
article.setPhoto(UrlPhoto);
return articelService.saveArticle(article);
}
}
lickr.apiKey=2d5de463eea9a98a698ad01861cbb58f
flickr.apiSecret=a2eb4b637b8f9c60
flickr.appKey=72157720903774158-1d3546a3852db77b
flickr.appSecret=9283c70e54b9a6cb
and this is URl testing with postman:http://localhost:8080/photo/save/1/aymen/article