Get the public URL of a private Flickr file in Java

Do you use Flickr? 1TB is also free, so you want to upload all the images and videos here! There is also an API, so it can be used nicely from the program ... When I tried and searched for it, there are quite a few ways to get public images, but surprisingly there aren't many ways to get private images ... So, I will post it instead of a memorandum (* ・ ω ・) ノ

Get API key

It won't start without this (> _ <) Please obtain from the following. https://www.flickr.com/services/apps/create/apply/

Obtaining OAuth Token and OAuth Token Secret

If you want to get a private image, you need an OAuth Token and an OAuth Token Secret. It will be necessary for uploading, so it will not hurt to get it (・ ∀ ・) Please refer to the following procedure to obtain it. Getting a Request Token

Run the code

with this

Are ready! Now it's time to get the code running.

Get Flickr4 Java

This time I will use the library. In case of gradle, add the following.

build.gradle


.....

dependencies {
	compile('com.flickr4java:flickr4java:2.17')
}

.....

Execution code generation

By generating an Auth class as shown below and setting it in RequestContext, you can access private files as well. Please read the $ {Api Key} part as appropriate for yourself.

Main.java


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;

public class Main {
    public static void main(String... args) throws FlickrException {
        final Flickr flickr = new Flickr(
                "${Api Key}",
                "${Shared Secret}",
                new REST()
        );

        Auth auth = new Auth();
        auth.setToken("${OAuth Token}");
        auth.setTokenSecret("${OAuth Token Secret}");
        RequestContext requestContext = RequestContext.getRequestContext();
        requestContext.setAuth(auth);

        PhotosetsInterface photosetsInterface = flickr.getPhotosetsInterface();
    }
}

Public URL of private file

You can get the public URL by formatting it referring to the following. https://www.flickr.com/services/api/misc.urls.html

By doing this, you can get all the small size (320px) images of API users regardless of whether they are public or private.

Main.java


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.photosets.Photoset;
import com.flickr4java.flickr.photosets.Photosets;
import com.flickr4java.flickr.photosets.PhotosetsInterface;

public class Main {

    private static final String IMAGE_URL = "https://farm%s.staticflickr.com/%s/%s_%s_%s.jpg ";

    public static void main(String... args) throws FlickrException {
        final Flickr flickr = new Flickr(
                "${Api Key}",
                "${Shared Secret}",
                new REST()
        );

        Auth auth = new Auth();
        auth.setToken("${OAuth Token}");
        auth.setTokenSecret("${OAuth Token Secret}");
        RequestContext requestContext = RequestContext.getRequestContext();
        requestContext.setAuth(auth);

        PhotosetsInterface photosetsInterface = flickr.getPhotosetsInterface();
        Photosets photosets = photosetsInterface.getList(null);

        photosets.getPhotosets().stream().map(Photoset::getId).flatMap(id -> {
            try {
                return photosetsInterface.getPhotos(id, 0, 0).stream();
            } catch (FlickrException e) {
                throw new RuntimeException();
            }
        }).forEach(photo -> System.out.println(
                String.format(IMAGE_URL, photo.getFarm(), photo.getServer(), photo.getId(), photo.getSecret(), "n")
                )
        );
    }
}

Miscellaneous feelings

I was able to get the public URL of a private file with a relatively simple code! If it is a personal application, I think you can use this kind of processing ♪

Recommended Posts

Get the public URL of a private Flickr file in Java
[Java] Get the file in the jar regardless of the environment
Get the result of POST in Java
How to get the length of an audio file in java
Measure the size of a folder in Java
The story of forgetting to close a file in Java and failing
Sample program that returns the hash value of a file in Java
How to get the absolute path of a directory running in Java
[Java] How to get the URL of the transition source
Source used to get the redirect source URL in Java
[Java] Get the file path in the folder with List
Get the URL of the HTTP redirect destination in Ruby
[Java] Integer information of characters in a text file acquired by the read () method
A quick explanation of the five types of static in Java
Activate Excel file A1 cell of each sheet in Java
Unzip the zip file in Java
Get stuck in a Java primer
I want to find the MD5 checksum of a file in Java and get the result as a string in hexadecimal notation.
[Java] Get the dates of the past Monday and Sunday in order
Get a proxy instance of the component itself in Spring Boot
[Java] Returns a Japanese name file in filename of HTTP header
Let's create a TODO application in Java 5 Switch the display of TODO
How to get the date in java
The story of writing Java in Emacs
[Java8] Search the directory and get the file
Dynamically increase the number of elements in a Java 2D array (multidimensional array)
Memo: [Java] If a file is in the monitored directory, process it.
How to find out the Java version of a compiled class file
[Java] How to get to the front of a specific string using the String class
[Java] Get MimeType from the contents of the file with Apathce Tika [Kotlin]
The story of low-level string comparison in Java
[Java] Handling of JavaBeans in the method chain
JAVA: jar, aar, view the contents of the file
The story of making ordinary Othello in Java
Read a string in a PDF file with Java
About the idea of anonymous classes in Java
A story about the JDK in the Java 11 era
[Java] Access the signed URL of s3 (signed version 2)
The story of learning Java in the first programming
[Java] Get the length of the surrogate pair string
[JAVA] Get only the file name, excluding the extension
Feel the passage of time even in Java
A bat file that uses Java in windows
[Java] Read the file in src / main / resources
A quick review of Java learned in class
[Java] How to get the redirected final URL
[Java] How to get the authority of the folder
Import files of the same hierarchy in Java
How to get the ID of a user authenticated with Firebase in Swift
Validate the identity token of a user authenticated with AWS Cognito in Java
Sample code to get the values of major SQL types in Java + MySQL 8.0
Count the number of occurrences of a string in Ruby
Get a list of MBean information for Java applications
A quick review of Java learned in class part4
[Java] Check the JDK version of the built war file
Get the name of the test case in the JUnit test class
[Java] How to get the maximum value of HashMap
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
How to save a file with the specified extension under the directory specified in Java to the list
A note for Initializing Fields in the Java tutorial
Get a list of classes in a Guava specific package