Skip to content
Justin edited this page Nov 15, 2020 · 7 revisions

instagram4j

Wiki for instagram4j a wrapper for Instagram's undocumented API in Java

Quick Usage

Login:

IGClient client = IGClient.builder()
        .username("username")
        .password("password")
        .login();

Actions:

IGClient client = ...

client.actions()
.timeline()
.uploadPhoto(new File("myPhoto.jpg"), "My Caption")
.thenAccept(response -> {
    System.out.println("Successfully uploaded photo!");
})
.join(); // block current thread until complete

Executing requests:

IGClient client = ...

// Alternatively use client.sendRequest
new FeedTimelineRequest().execute(client)
.thenAccept(response -> {
    response.getFeed_items().forEach(...);
})
.join(); // block current thread until complete

IGClient Actions

This library contains currently a very limited wrapper for common actions. Such actions usually are composed of multiple requests (e.g. uploading a photo), so for convenience the action is wrapped into a single call. Common requests are also wrapped. This is very experimental and subject to design changes at any time.

Example:

Uploading a photo to your timeline using the fluent wrapper

IGClient client = ...

client.actions()
.timeline()
.uploadPhoto(new File("myPhoto.jpg"), "My Caption")
.thenAccept(response -> {
    System.out.println("Successfully uploaded photo!");
})
.join(); // block current thread until complete

IGRequest

IGRequest is the base abstract class for IGPostRequest and IGGetRequest, similar to the previous version. These abstract classes allow for requests to conform to a "standard" for maintainability and readability. The idea is for developers to easily make new requests for new endpoints in the future. IGRequest objects are passed into the sendRequest method in IGClient to be executed and parsed into its corresponding response or view. For convenience, IGRequest objects have an execute method that takes in IGClient to execute the request. Both methods can send requests asynchronously and both return a ComletableFuture. Additionally, formRequest in IGRequest objects may be used to form a Request to be executed by OkHttpClient.

IGPostRequest

For POST actions in the Instagram app, a JSON payload is used. The library makes use of jackson-databind to serialize POJO into JSON. In previous versions, the payload had to be "signed". However in newer versions, Instagram does not seem to require that anymore. Some requests do still require a url encoded form with "SIGNATURE" as the signature.

Example:

IGClient client = ...
FeedTimelineRequest request = new FeedTimelineRequest();
client.sendRequest(request)
.thenAccept(repsonse -> {
    // Response
})
.join(); // block current thread until completion
IGClient client = ...
FeedTimelineRequest request = new FeedTimelineRequest();
// Alternatively use execute
request.execute(client)
.thenAccept(response -> {
    // Response
})
.join(); // block current thread until completion