// https://www.computerbase.de/forum/threads/instagram-java-api.1568143/
import java.util.Map;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.jsoup.Connection;
public class IGLogin
{
private static final String USER_AGENT= "Mozilla/5.0";
//private static final String USER_AGENT= "Mozilla/5.0 (Windows NT 6.1; rv:45.0) Gecko/20100101 Firefox/45.0";
private static final String mainURL = "https://www.instagram.com/";
private static final String loginURL= "https://www.instagram.com/accounts/login/ajax/";
public static void main(String[] args) throws Exception
{
Connection.Response mainPage= Jsoup.connect(mainURL).userAgent(USER_AGENT).method(Connection.Method.GET).execute();
Map<String, String> cookies= mainPage.cookies();
System.out.println("Cookies: " + cookies);
// curl "https://www.instagram.com/accounts/login/ajax/"
// -H "Host: www.instagram.com"
// -H "User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:45.0) Gecko/20100101 Firefox/45.0"
// -H "Accept: */*"
// -H "Accept-Language: en-US,en;q=0.5"
// -H "Accept-Encoding: gzip, deflate"
// -H "Content-Type: application/x-www-form-urlencoded; charset=UTF-8"
// -H "X-Instagram-AJAX: 1"
// -H "X-CSRFToken: 985b80f0ab6b57b6ff07c591d664796e"
// -H "X-Requested-With: XMLHttpRequest"
// -H "Cookie: csrftoken=985b80f0ab6b57b6ff07c591d664796e; mid=VubN7AAEAAGkESX--tjaAD22umf6"
// -H "Connection: keep-alive"
// -H "Pragma: no-cache"
// -H "Cache-Control: no-cache"
// --data "username=mylogin&password=mypassword"
Connection c= Jsoup.connect(loginURL).userAgent(USER_AGENT);
c.header("X-Requested-With", "XMLHttpRequest");
c.header("Pragma", "no-cache");
c.header("Cache-Control", "no-cache");
c.header("X-Instagram-AJAX", "1");
c.header("X-CSRFToken", cookies.get("csrftoken") );
c.data("username", "mylogin", "password", "mypassword");
Connection.Response login= c.cookies(cookies).method(Connection.Method.POST).execute();
System.out.println("status code= " + login.statusCode() );
}
}