instagram4j

by instagram4j

instagram4j / instagram4j

:camera: Java port to Instagram's private API

552 Stars 216 Forks Last release: Not found Apache License 2.0 507 Commits 15 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:

instagram4j

Download Build Status Apache License Gitter

:camera: Java wrapper using OkHttpClient for Instagram's private api (Android emulation)

Table of contents

Install

There is currently no release yet from central repository.

Download latest develop build: Download

Dev builds require the bintray repository be added. Dev builds are highly experimental and subject to breaking changes between versions.

Example for gradle:

repositories {
    maven {
        url 'https://dl.bintray.com/instagram4j/maven'
    }
    jcenter()
}

If you want to keep up with the latest snapshot of the develop branch instead of a develop release, use JitPack

Example for gradle:

repositories {
    maven {
        url 'https://jitpack.io'
    }
}
dependencies {
    implementation 'com.github.instagram4j:instagram4j:develop-SNAPSHOT'
}

Requirements

This project depends on - Java 8+ - okhttpclient - okhttpclient url connection - jackson data-bind - jackson annotations - slf4j-api - apache commons codec

Overview

This Java library provides requests that emulate the Android Instagram app. Most of the official app functionality is supported here. This library has undergone a massive rewrite (instagram4j 1.x.x is not compatible) The rewrite intends to help with maintainability and flexibility throughout time.

Features

Most of the Android Instagram app features are supported. Here are some notable ones. - OkHttpClient and jackson-databind - two factor login - support for challenges - iterable feeds - serialization - timeline, story, live, direct messaging, shopping, and more!

Usage

Terms and Conditions

This library is intended for personal use and for educational experiences due to limitations of Instagram's public API.

  • Please use Instagram's public API if possible
  • Do not use this library to spam (botting, spam messaging, etc...)
  • There will be no support for those with malicious intent
  • Use reasonable (human) delay in between sending requests
  • Don't be evil.

This library is in no way affiliated with, authorized, maintained, sponsored or endorsed by Instagram or any of its affiliates or subsidiaries. This is an independent and unofficial API. Use at your own risk.

Contributors are not responsible for usage and maintainability. Due to the nature of this project, some features of the library are not guaranteed as they make change and break in the future. This library is licensed under ASL.


Quick Usage

Login:

java
IGClient client = IGClient.builder()
        .username("username")
        .password("password")
        .login();
Actions: ```java 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:**
java IGClient client = ...

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

Setup and Login

An IGClient instance must be constructed and logged in to send most requests.

Simple Login

Basic login using IGClient builder method.

Example:

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

Example:

// simulate pre login flow (synchronous) and post login flow (asynchronous)
IGClient client = IGClient.builder()
        .username("username")
        .password("password")
        .simulatedLogin();

Two factor login

Provide a consumer that may resolve two factor login process. The example uses the included utility to resolve two factor logins.

Example:

Scanner scanner = new Scanner(System.in);

// Callable that returns inputted code from System.in Callable inputCode = () -> { System.out.print("Please input code: "); return scanner.nextLine(); };

// handler for two factor login LoginHandler twoFactorHandler = (client, response) -> { // included utility to resolve two factor // may specify retries. default is 3 return IGChallengeUtils.resolveTwoFactor(client, response, inputCode); };

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

Challenge login

Sometimes a challenge may arise when logging in. Provide a LoginHandler to handle challenges. The example uses the included utility to handle challenges automatically.

Example:

Scanner scanner = new Scanner(System.in);

// Callable that returns inputted code from System.in Callable inputCode = () -> { System.out.print("Please input code: "); return scanner.nextLine(); };

// handler for challenge login LoginHandler challengeHandler = (client, response) -> { // included utility to resolve challenges // may specify retries. default is 3 return IGChallengeUtils.resolve(client, response, inputCode); };

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

Login with proxy

You may provide Proxy and Authenticator for the Proxy thru configuring an OkHttpClient and passing it in thru the builder.

java
OkHttpClient httpClient = new OkHttpClient.Builder().proxy(...).build();
IGClient client = IGClient.builder()
        .username("username")
        .password("password")
        .client(httpClient)
        .login();

Sending requests and actions

This library provides a limited wrapper api that may be used to locate and send common requests. Not all features are supported through actions currently. Actual requests are located under the requests package and can be sent through IGClient like in previous versions. Requests can be sent asynchronously. All requests return a CompletableFuture.

Simple Example:

IGClient client = ...

client.actions().timelime() .uploadPhoto(new File("myPhoto.jpg"), "My Caption") .thenAccept(res -> { // perform actions with response log.info("Uploaded photo {}", response.getMedia().getId()); }) .exceptionally(tr -> { // something has terribly gone wrong! // handle exception

return null;

}) .join(); // blocks currect thread until completion

Chainng Example:

IGClient client = ...

// finds user by username then gets friendship status client.actions().users() .findByUsername("instagram") .thenCompose(UserAction::getFriendship) .thenAccept(friendship -> { // response here }) .join();

// uploads a photo then comments on it client.actions().timeline() .uploadPhoto(new File("myPhoto.jpg"), "My Caption") .thenCompose(response -> { // action with response return new MediaCommentRequest(response.getMedia().getId(), "First comment!").execute(client) }) .join();

Serialization

IGClient is a Serializable object that can be saved and later reconstructed. Session cookies however must be separately serialized. Session cookies for OkHttpClient are done through an implementation of CookieJar. You may provide your own implementation of a serializable cookie jar and then serialize your cookies for later use. Session cookies are good for 90 days and avoids relogins.

See example for serialization and deserialization here.

Contributing

See Issues tab to find good starting issues to work on. If you have an addition you would like to make, please do not hesitate to make a pull request!

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.