Ejemplo n.º 1
0
        private static AuthInfo GetInstagramAuth(string oAuthUri, string clientId, string redirectUri,
            InstagramConfig config, string login, string password)
        {
            List<Auth.Scope> scopes = new List<Auth.Scope>();
            scopes.Add(Auth.Scope.basic);

            var link = InstaSharp.Auth.AuthLink(oAuthUri, clientId, redirectUri, scopes);

            // User name in the specified node
            CookieAwareWebClient client = new CookieAwareWebClient();
            // We went to the login page
            var result = client.DownloadData(link);
            var html = System.Text.Encoding.Default.GetString(result);

            // Take the token
            string csr = "";
            string pattern = @"csrfmiddlewaretoken""\svalue=""(.+)""";
            var r = new System.Text.RegularExpressions.Regex(pattern);
            var m = r.Match(html);
            csr = m.Groups[1].Value;

            // Login
            string loginLink = string.Format(
                "https://instagram.com/accounts/login/?next=/oauth/authorize/%3Fclient_id%3D{0}%26redirect_uri%3Dhttp%3A//www.pwr.wroc.pl%26response_type%3Dcode%26scope%3Dbasic", clientId);

            NameValueCollection parameters = new NameValueCollection();
            parameters.Add("csrfmiddlewaretoken", csr);
            parameters.Add("username", login);
            parameters.Add("password", password);

            // You need to add the secret cookies received pre-login

            // Looking for something headers
            string agent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)";
            client.Headers["Referer"] = loginLink;
            client.Headers["Host"] = "instagram.com";
            client.Headers["Content-Type"] = "application/x-www-form-urlencoded";
            client.Headers["User-Agent"] = agent;
            client.Headers["Accept-Language"] = "pl-PL";
            client.Headers["Accept"] = "text/html, application/xhtml+xml, */*";
            client.Headers["Cache-Control"] = "no-cache";

            // request
            var result2 = client.UploadValues(loginLink, "POST", parameters);

            // Fasting data received code
            // New link is not on the api, and on instagram
            string newPostLink = string.Format(
                "https://instagram.com/oauth/authorize/?client_id={0}&redirect_uri=http://www.pwr.wroc.pl&response_type=code&scope=basic", clientId);

            HttpWebRequest request =
                (HttpWebRequest)WebRequest.Create(newPostLink);
            request.AllowAutoRedirect = false;
            request.CookieContainer = client.CookieContainer;
            request.Referer = newPostLink;
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.UserAgent = agent;

            string postData = string.Format("csrfmiddlewaretoken={0}&allow=Authorize", csr);
            request.ContentLength = postData.Length;

            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] loginDataBytes = encoding.GetBytes(postData);
            request.ContentLength = loginDataBytes.Length;
            Stream stream = request.GetRequestStream();
            stream.Write(loginDataBytes, 0, loginDataBytes.Length);

            // send the request
            var response = request.GetResponse();
            string location = response.Headers["Location"];

            // Now take out the code and get the authentication
            pattern = @"www.pwr.wroc.pl\?code=(.+)";
            r = new System.Text.RegularExpressions.Regex(pattern);
            m = r.Match(location);
            string code = m.Groups[1].Value;

            // Finally, we get an authentication token
            var auth = new InstaSharp.Auth(config); //.OAuth(InstaSharpConfig.config);

            // now we have to call back to instagram and include the code they gave us
            // along with our client secret
            var oauthResponse = auth.RequestToken(code);

            return oauthResponse;
        }
Ejemplo n.º 2
0
 public static Auth GetAuth()
 {
     var auth = new Auth(GetConfig());
     return auth;
 }