Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class InstagramPrivateAPI
- {
- private class ReferenceString //потому что иначе строки передаются по значению
- {
- public string val = "";
- }
- private static string proxyPass = "";
- static InstagramPrivateAPI()
- {
- Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);
- }
- public static Random rand = new Random(DateTime.Now.Millisecond);
- public static CURLcode OnSSLContext(SSLContext ctx, Object extraData)
- {
- // To do anything useful with the SSLContext object, you'll need
- // to call the OpenSSL native methods on your own. So for this
- // demo, we just return what cURL is expecting.
- return CURLcode.CURLE_OK;
- }
- public static Int32 OnWriteData(Byte[] buf, Int32 size, Int32 nmemb,
- Object extraData)
- {
- //Console.Write(System.Text.Encoding.UTF8.GetString(buf));
- ((ReferenceString)extraData).val += System.Text.Encoding.UTF8.GetString(buf);
- return size * nmemb;
- }
- public static Int32 OnReadData(Byte[] buf, Int32 size, Int32 nmemb,
- Object extraData)
- {
- FileStream fs = (FileStream)extraData;
- return fs.Read(buf, 0, size * nmemb);
- }
- public static string SendRequest(string url, bool post, string post_data, string user_agent, bool cookies, string userid)
- {
- Easy.ReadFunction rf;
- FileStream fs;
- ReferenceString instagram_response = new ReferenceString();
- int d = 0;
- bool success = true;
- //Наша задача - попробовать сначала выполнить действие без авторизации, а если не получилось -
- //то его же, но уже после авторизации
- do
- {
- Easy easy = new Easy();
- Easy.WriteFunction wf = new Easy.WriteFunction(OnWriteData);
- easy.SetOpt(CURLoption.CURLOPT_URL, "https://i.instagram.com/api/v1/" + url);
- easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf);
- easy.SetOpt(CURLoption.CURLOPT_WRITEDATA, instagram_response);
- easy.SetOpt(CURLoption.CURLOPT_USERAGENT, user_agent);
- easy.SetOpt(CURLoption.CURLOPT_FOLLOWLOCATION, true);
- Easy.SSLContextFunction sf = new Easy.SSLContextFunction(OnSSLContext);
- easy.SetOpt(CURLoption.CURLOPT_SSL_CTX_FUNCTION, sf);
- easy.SetOpt(CURLoption.CURLOPT_CAINFO, "ca-bundle.crt");
- easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYPEER, false);
- if (post)
- {
- easy.SetOpt(CURLoption.CURLOPT_POST, true);
- easy.SetOpt(CURLoption.CURLOPT_POSTFIELDS, post_data);
- }
- //Для каждого аккаунта Instagram храним куки в отдельно файле
- if (cookies)
- {
- easy.SetOpt(CURLoption.CURLOPT_COOKIEFILE, string.Format("cookies{0}.txt", userid));
- }
- else
- {
- easy.SetOpt(CURLoption.CURLOPT_COOKIEJAR, string.Format("cookies{0}.txt", userid));
- }
- SeasideResearch.LibCurlNet.CURLcode response = easy.Perform();
- easy.GetInfo(CURLINFO.CURLINFO_HTTP_CONNECTCODE, ref d);
- easy.Cleanup();
- easy = null;
- // Curl.GlobalCleanup(); обойдемся без этого
- } while (!success);
- return instagram_response.val;
- }
- public static string GenerateGuid()
- {
- var rand = new System.Random();
- return string.Format("{0}{1}-{2}-{3}-{4}-{5}{6}{7}",
- rand.Next(0, 65535).ToString("x4"),
- rand.Next(0, 65535).ToString("x4"),
- rand.Next(0, 65535).ToString("x4"),
- rand.Next(16384, 20479).ToString("x4"),
- rand.Next(32768, 49151).ToString("x4"),
- rand.Next(0, 65535).ToString("x4"),
- rand.Next(0, 65535).ToString("x4"),
- rand.Next(0, 65535).ToString("x4"));
- }
- public static string GenerateUserAgent()
- {
- var resolutions = new string[] { "720x1280", "320x480", "480x800", "1024x768", "1280x720", "768x1024", "480x320" };
- var versions = new string[] { "GT-N7000", "SM-N9000", "GT-I9220", "GT-I9100" };
- var dpis = new string[] { "120", "160", "320", "240" };
- var ver = versions[new System.Random().Next(0, versions.Count() - 1)];
- var dpi = dpis[new System.Random().Next(0, dpis.Count() - 1)];
- var res = resolutions[new System.Random().Next(0, resolutions.Count() - 1)];
- return "Instagram 6.24.0" + " Android (" +
- new System.Random().Next(10, 11).ToString() + "/" +
- new System.Random().Next(1, 3).ToString() + "." +
- new System.Random().Next(3, 5).ToString() + "." + new System.Random().Next(0, 5).ToString() +
- "; " + dpi + "; " + res + "; samsung; " + ver + "; " + ver + "; smdkc210; en_US)";
- }
- public static string GenerateSignature(string data)
- {
- string secret = "25eace5393646842f0d0c3fb2ac7d3cfa15c052436ee86b5406a8433f54d24a5";
- var secretBytes = Encoding.UTF8.GetBytes(secret);
- var dataBytes = Encoding.UTF8.GetBytes(data);
- string signature = "";
- using (var hmac = new bouncy.System.Security.Cryptography.HMACSHA256(secretBytes))
- {
- var hash = hmac.ComputeHash(dataBytes);
- for (int i = 0; i < hash.Length; i++)
- {
- signature += hash[i].ToString("x2");
- }
- // signature = Convert.ToBase64String(hash);
- }
- return signature;
- }
- public static string datetimeToUnixEpoch(DateTime datetime)
- {
- DateTime sTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
- return ((int)(datetime - sTime).TotalSeconds).ToString();
- }
- public class InstaResponse
- {
- public string status;
- public string media_id;
- }
- public static bool login(string userid, string username, string password)
- {
- // Define the GuID
- string guid = GenerateGuid();
- // Define the user agent
- string agent = GenerateUserAgent();
- // Set the devide ID
- string device_id = "android-" + guid;
- /* LOG IN */
- // You must be logged in to the account that you wish to post a photo too
- // Set all of the parameters in the string, and then sign it with their API key using SHA-256
- string data = "{\"device_id\":\"" + device_id + "\",\"guid\":\"" + guid + "\",\"username\":\"" +
- username + "\",\"password\":\"" + password + "\",\"Content-Type\":\"application/" +
- "x-www-form-urlencoded; charset=UTF-8\"}";
- var sig = GenerateSignature(data);
- InstaResponse obj = null;
- data = "signed_body=" + sig + "." + System.Web.HttpUtility.UrlEncode(data) + "&ig_sig_key_version=6";
- string login = SendRequest("accounts/login/", true, data, agent, false, userid)[0];
- if (login.StartsWith("Sorry, an error occurred while processing this request."))
- {
- return false;
- }
- else
- {
- if (string.IsNullOrEmpty(login))
- {
- return false;
- }
- else
- {
- // Decode the array that is returned
- try
- {
- obj = Newtonsoft.Json.JsonConvert.DeserializeObject<InstaResponse>(login);
- var status = obj.status;
- if (status == "ok") return true;
- }
- catch
- {
- }
- }
- }
- return false;
- }
- }
Add Comment
Please, Sign In to add comment