There is nothing especially difficult in form authentication. You can not even parse the page, and take the csrf-token from cookies.
The class used for authentication:
using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Net.Http; using System.Threading.Tasks; namespace InstagramClient { public class Instagram : IDisposable { private const string USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) "+ "AppleWebKit/537.36 (KHTML, like Gecko) "+ "Chrome/45.0.2414.0 Safari/537.36"; private HttpClientHandler m_handler; private HttpClient m_client; /// <summary> /// Http-клиент, который после аунтификации /// можно использовать для выполнения /// различных действий на сайте . /// </summary> public HttpClient Client { get { return m_client; } } public Instagram() { m_handler = new HttpClientHandler(); m_client = new HttpClient(m_handler); m_client.BaseAddress = new Uri("https://instagram.com/"); m_client.DefaultRequestHeaders.UserAgent.ParseAdd(USER_AGENT); } public void Dispose() { m_client.Dispose(); m_handler.Dispose(); } /// <summary> /// Осуществляет вход в Instagram, /// аналогично использованию стандартной формы логина в браузере /// </summary> /// <param name="username">Логин либо e-mail</param> /// <param name="password">Пароль</param> /// <returns></returns> public async Task<bool> LoginAsync(string username, string password) { // получаем страницу входа, что бы сайт установил Cookie 'csrftoken' // содержимое страницы нам не важно await m_client.GetAsync("/accounts/login/"); // получаем токен из Cookies var cookies = m_handler.CookieContainer.GetCookies(m_client.BaseAddress); var csrftoken = cookies["csrftoken"].Value; // готовим поля для формы входа var fields = new Dictionary<string, string>() { { "username", username }, { "password", password } }; // готовим запрос var request = new HttpRequestMessage(HttpMethod.Post, "/accounts/login/ajax/"); request.Content = new FormUrlEncodedContent(fields); request.Headers.Referrer = new Uri(m_client.BaseAddress, "/accounts/login/"); // Дополнительные заголовки запроса. // Кроме X-CSRFToken, остальное в общем-то не обязательно. request.Headers.Add("X-CSRFToken", csrftoken); request.Headers.Add("X-Instagram-AJAX", "1"); request.Headers.Add("X-Requested-With", "XMLHttpRequest"); // Авторзуемся через AJAX var response = await m_client.SendAsync(request); var info = JsonConvert.DeserializeObject<LoginInfo>(await response.Content.ReadAsStringAsync()); return info.authenticated; } private class LoginInfo { public string status { get; set; } public bool authenticated { get; set; } } } }
Well, in case of successful authentication, we use Client to receive / send the information we need:
using (var instagram = new Instagram()) { if (await instagram.LoginAsync("User", "Pa$$word")) { var homepage = await instagram.Client.GetStringAsync("/"); /* ... */ } }
Well, about the API:
register yourself
register a new client (ibid. specify redirect-uri)
see the documentation and call the methods you need
Redirect-uri when authorizing sites is equal to the handler on the server. When authorizing applications, it can be practically equal to anything (even if it does not exist, if you have control over client requests), the main thing is that you could pull ACCESS-TOKEN from the URL to which instagram will redirect you.