How to access private photos through Flickrj Api?

I am making an authenticated call to access photos through the Flickr API. But I only get my public photos, but not private photos.

Below is the code I'm using,

Flickr f; RequestContext requestContext; String frob = ""; String token = ""; DocumentBuilder xmlParser = null; public void getImages() throws ParserConfigurationException, IOException, SAXException, FlickrException, URISyntaxException, NoSuchAlgorithmException { DocumentBuilderFactory dcb = DocumentBuilderFactory.newInstance(); try { this.xmlParser = dcb.newDocumentBuilder(); } catch (ParserConfigurationException ex) { ex.printStackTrace(); } f = new Flickr("199d038ad88f6c6c377a4ab2341fb60f","4583b2386d3d6439",new REST()) ; Flickr.debugStream = false; requestContext = RequestContext.getRequestContext(); AuthInterface authInterface = f.getAuthInterface(); //PeopleInterface peopleInterface = f.getPeopleInterface(); try { frob = authInterface.getFrob(); } catch (FlickrException e) { e.printStackTrace(); } System.out.println("frob: " + frob); java.net.URL url =authInterface.buildAuthenticationUrl(Permission.READ, frob); System.out.println(url.toExternalForm()); Desktop desktop = Desktop.getDesktop(); desktop.browse(url.toURI()); // Get the response Auth auth = null ; String aLine = ""; while(aLine.equals("")) { java.io.DataInputStream in = new java.io.DataInputStream(System.in); aLine = in.readLine(); } auth =authInterface.getToken(frob); System.out.println("auth = "+auth); requestContext = RequestContext.getRequestContext(); requestContext.setAuth(auth); f.setAuth(auth); UrlsInterface urlsInterface = f.getUrlsInterface(); PhotosInterface photosInterface = f.getPhotosInterface(); SearchParameters searchParams=new SearchParameters(); searchParams.setSort(SearchParameters.INTERESTINGNESS_DESC); //Execute search with entered tags searchParams.setUserId(auth.getUser().getId()); PhotoList photoList=photosInterface.search(searchParams, 10,1); if(photoList!=null){ //Get search result and check the size of photo result for(int i=0;i<photoList.size();i++){ Photo photo=(Photo)photoList.get(i); System.out.println(photo.getSmallSquareUrl()); } } 
+4
source share
4 answers

I managed to solve this problem by following a different approach.

This is how I solved it. Instead of using the GET method, I used the getList method in photoSetsInterface (photoSetsInterface.getList (auth.getUser (). GetId ()). GetPhotosets ()). For this method, you can pass auth_token as an input parameter. Therefore, he gives you all sets of photographs. Then I took each photoset and extracted images at all levels of privacy.

And then you can take all the photos that are not in the set using the getNotInSet method in PhotosInterface. The getNotInSet method returns all photos that are not in the set, regardless of the level of privacy.

You can find sample code on blog .

0
source

Access to personal data is possible by editing flickrj.

I added the auth_token parameter to com.aetrion.flickr.photos.PhotosInterface :

 public PhotoList search(SearchParameters params, int perPage, int page) throws IOException, SAXException, FlickrException { PhotoList photos = new PhotoList(); List parameters = new ArrayList(); parameters.add(new Parameter("method", METHOD_SEARCH)); parameters.add(new Parameter("api_key", apiKey)); parameters.addAll(params.getAsParameters()); if (perPage > 0) { parameters.add(new Parameter("per_page", "" + perPage)); } if (page > 0) { parameters.add(new Parameter("page", "" + page)); } // String token = RequestContext.getRequestContext().getAuth().getToken(); if (token != null) parameters.add( new Parameter( "auth_token", RequestContext.getRequestContext().getAuth().getToken() ) ); // parameters.add( new Parameter( "api_sig", AuthUtilities.getSignature(sharedSecret, parameters) ) ); Response response = transport.get(transport.getPath(), parameters); 

With this change, I was able to get my personal photos.

flickr api docs say that "for each authenticated call, both auth_token and api_sig arguments are required" .. and this was absent.

I took this idea from there: link text and used the servlet to access flickr.

+1
source

To make authenticated calls using flickrj api, you need, in addition to your apiKey and secret, a third code, a token .

We get the token with a one-time first call, and then with the token in your hand, you can simply use the api classes, as you would expect.

If you have ever used the flickr iOS / Android client, you will find out: the first time you use the application, it will send you a URL for you to authorize. What does he do when you authorize him, he gets this token , and then it's good to go. Without it, he is not. The same goes for your java application.

I used this example implementation for this first step authentication with a few modifications, though, since I am working in a Spring MVC application, not a desktop application. Remember that you only run once, and the only point to this is to get the token key.

https://github.com/mohlendo/flickrj/blob/master/examples/AuthExample.java

Line 55:
frob = authInterface.getFrob();

The sole purpose of frob is to use it to create an authentication URL, see line 60:
URL url = authInterface.buildAuthenticationUrl(Permission.DELETE, frob);

I did this in a Spring MVC application, not a desktop application, so after line 60, I instead typed the URL into System.out and then paused the program for a minute using Thread.sleep(60000);

This gave me enough time to copy the URL from my console to the browser and click "OK so my application can use my flickr account."

Right after that, when the program resumes everything I cared about, there was line 70 that prints the token on the console.

With a token in my hand, authorization is complete, and I was configured to use the Flickrj api.

With three keys in hand (apiKey, secret, token) here is my Service class with a static method for creating a Flickr object.

 import java.io.IOException; import java.io.InputStream; import java.util.Properties; import javax.xml.parsers.ParserConfigurationException; import com.aetrion.flickr.Flickr; import com.aetrion.flickr.REST; import com.aetrion.flickr.RequestContext; import com.aetrion.flickr.auth.Auth; import com.aetrion.flickr.auth.Permission; import com.aetrion.flickr.util.IOUtilities; public class FlickrjService { private static Flickr flickr; public static Flickr getFlickr() throws ParserConfigurationException, IOException { InputStream in = null; Properties properties; try { in = FlickrjService.class.getResourceAsStream("/flickrj.properties"); properties = new Properties(); properties.load(in); } finally { IOUtilities.close(in); } flickr = new Flickr(properties.getProperty("flickrj.apiKey"), properties.getProperty("flickrj.secret"), new REST("www.flickr.com")); RequestContext requestContext = RequestContext.getRequestContext(); Auth auth = new Auth(); auth.setPermission(Permission.READ); auth.setToken(properties.getProperty("flickrj.token")); requestContext.setAuth(auth); Flickr.debugRequest = false; Flickr.debugStream = false; return flickr; } } 
+1
source

I don’t know if this will help you, but I had a similar problem with viewing only public photos and since then I got a job.

I downloaded flickrapi-1.2.zip from: http://sourceforge.net/projects/flickrj/files/ and used flickrapi-1.2.jar in my webapp.

Flickr.debugRequest showed a GET request, but it never included the parameters "auth_token", "api_sig", etc., which AuthInterface checkToken (for example) successfully forcibly executes the request.

Anyway, so today I downloaded the source from:

flickrj.cvs .sourceforge.net / flickrj /

(You can view the change history at: flickrj.cvs .sourceforge.net / viewvc / flickrj / api / build.properties? View = log, if you're interested.)

I built the jar locally, included it in a web application and could see private photos.

I did not take the time to research further information about exactly what the problem was ... you need to use it for something :)

I do not know if this will help you, but this find is the only thing that separates me from madness.

0
source

Source: https://habr.com/ru/post/1316274/


All Articles