Ejemplo n.º 1
0
        private token GetDict(string text)
        {
            Dictionary <string, string> d = new Dictionary <string, string>();

            text = text.Replace(" ", "");
            string[]     words        = text.Split('\n');
            token        token        = new token();
            tokencache   tokencache   = new tokencache();
            accesstoken  accesstoken  = new accesstoken();
            requesttoken requesttoken = new requesttoken();

            requesttoken.token    = words[3].Split(':', ',')[1].Substring(1, words[3].Split(':', ',')[1].Length - 2);
            requesttoken.secret   = words[4].Split(':', ',')[1].Substring(1, words[4].Split(':', ',')[1].Length - 2);
            requesttoken.verifier = words[5].Split(':')[1].Substring(1, words[5].Split(':', ',')[1].Length - 3);
            try
            {
                accesstoken.token      = words[8].Split(':', ',')[1].Substring(1, words[8].Split(':', ',')[1].Length - 2);
                accesstoken.secret     = words[9].Split(':', ',')[1].Substring(1, words[9].Split(':', ',')[1].Length - 2);
                accesstoken.endpoint   = words[10].Split(':')[1].Substring(1, words[10].Split(':', ',')[1].Length - 1) + ":" + words[10].Split(':')[2] + ":" + words[10].Split(':')[3].Substring(0, words[10].Split(':', ',')[3].Length - 2);
                accesstoken.revokecode = words[11].Split(':', ',')[1].Substring(1, words[11].Split(':', ',')[1].Length - 3);
                tokencache.accesstoken = accesstoken;
            }
            catch
            {
                tokencache.accesstoken = null;
            }
            tokencache.requesttoken = requesttoken;
            token._ = tokencache;
            return(token);
        }
Ejemplo n.º 2
0
 public Microgear()
 {
     this.onDisconnect = do_nothing;
     this.onPresent    = do_nothing;
     this.onAbsent     = do_nothing;
     this.onConnect    = do_nothing;
     this.onMessage    = do_nothing;
     this.onError      = do_nothing;
     this.cache        = new cache();
     this.tokencache   = new tokencache();
     this.accesstoken  = new accesstoken();
     this.requesttoken = new requesttoken();
     this.token        = new token();
 }
Ejemplo n.º 3
0
        private void forToken()
        {
            var verifier = "";
            var path     = "";

            if (init.gearalias != null)
            {
                verifier = init.gearalias;
                path     = "response_type=code&client_id=" + init.gearkey + "&scope=appid:" + init.appid + "%20alias:" + init.gearalias + "&state=mgrev:" + init.mgrev;
            }
            else
            {
                verifier = init.mgrev;
                path     = "response_type=code&client_id=" + init.gearkey + "&scope=appid:" + init.appid + "&state=mgrev:" + init.mgrev;
            }
            var    client       = new RestClient(init.gearauthsite + "/oauth2/authorize?" + path);
            var    request      = new RestRequest(Method.GET);
            var    response     = client.Execute(request);
            string responsecode = response.ResponseUri.ToString();

            string[] responsecodelist = responsecode.Replace("code=", "|").Split('|');
            if (responsecodelist.Length == 2)
            {
                var    code             = responsecodelist[1];
                string oauthToken       = code;
                string oauthTokenSecret = "null";
                this.requesttoken.token      = oauthToken;
                this.requesttoken.secret     = oauthTokenSecret;
                this.requesttoken.verifier   = verifier;
                this.tokencache.requesttoken = this.requesttoken;
                path     = "grant_type=authorization_code&code=" + code + "&client_id=" + init.gearkey + "&client_secret=" + init.gearsecret + "&state=mgrev:" + init.mgrev;
                client   = new RestClient(init.gearauthsite + "/oauth2/token?" + path);
                request  = new RestRequest(Method.POST);
                response = client.Execute(request);
                string   responsetoken     = response.Content;
                string[] responsetokenlist = responsetoken.Replace("{\"access_token\":\"", "|").Split('|');
                if (responsetokenlist.Length == 2)
                {
                    responsetokenlist = responsetokenlist[1].Replace("\",\"token_type\"", "|").Split('|');
                    if (responsetokenlist.Length == 2)
                    {
                        responsetokenlist = responsetokenlist[0].Replace(":", "|").Split('|');
                        oauthToken        = responsetokenlist[0];
                        oauthTokenSecret  = responsetokenlist[1];
                        responsetokenlist = responsetoken.Replace("\"endpoint\":\"", "|").Split('|');
                        responsetokenlist = responsetokenlist[1].Replace("\"}", "|").Split('|');
                        string endpoint   = responsetokenlist[0];
                        string revokecode = CreateToken(oauthTokenSecret + "&" + init.gearsecret, oauthToken);
                        revokecode                  = revokecode.Replace('/', '_');
                        this.accesstoken.token      = oauthToken;
                        this.accesstoken.secret     = oauthTokenSecret;
                        this.accesstoken.endpoint   = endpoint;
                        this.accesstoken.revokecode = revokecode;
                        this.tokencache.accesstoken = this.accesstoken;
                        this.tokencache.key         = init.gearkey;
                        this.token._                = this.tokencache;
                        this.cache.set_item(this.token, "microgear-" + init.gearkey + ".cache");
                    }
                }
                else
                {
                    this.onError("Access token is not issued, please check your consumerkey and consumersecret.");
                    reset = true;
                    this.ResetToken();
                }
            }
            else
            {
                this.onError("Request token is not issued, please check your appkey and appsecret.");
            }
        }
Ejemplo n.º 4
0
        private void forToken()
        {
            var verifier = "";

            if (init.gearalias != null)
            {
                verifier = init.gearalias;
            }
            else
            {
                verifier = init.mgrev;
            }
            Uri        baseUrl = new Uri(init.gearauthsite);
            RestClient client  = new RestClient(baseUrl.AbsoluteUri)
            {
                Authenticator = OAuth1Authenticator.ForRequestToken(init.gearkey, init.gearsecret)
            };
            RestRequest request = new RestRequest(init.requesttokenendpoint, Method.POST);

            request.AddParameter("oauth_callback", "scope=" + init.scope + "&mgrev=" + init.mgrev + "&appid=" + init.appid + "&verifier=" + verifier);
            Int32          unixTimestamp     = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
            IRestResponse  response          = client.Execute(request);
            HttpStatusCode statusCode        = response.StatusCode;
            int            numericStatusCode = (int)statusCode;

            if (numericStatusCode == 200)
            {
                NameValueCollection qs  = HttpUtility.ParseQueryString(response.Content);
                string oauthToken       = qs["oauth_token"];
                string oauthTokenSecret = qs["oauth_token_secret"];
                this.requesttoken.token      = oauthToken;
                this.requesttoken.secret     = oauthTokenSecret;
                this.requesttoken.verifier   = verifier;
                this.tokencache.requesttoken = this.requesttoken;
                string url = client.BuildUri(request).ToString();
                request = new RestRequest(init.accesstokenendpoint, Method.POST);
                request.RequestFormat = DataFormat.Json;
                client.Authenticator  = OAuth1Authenticator.ForAccessToken(init.gearkey, init.gearsecret, oauthToken,
                                                                           oauthTokenSecret);
                request.AddParameter("oauth_verifier", verifier);
                response          = client.Execute(request);
                statusCode        = response.StatusCode;
                numericStatusCode = (int)statusCode;
                if (numericStatusCode == 200)
                {
                    qs               = HttpUtility.ParseQueryString(response.Content);
                    oauthToken       = qs["oauth_token"];
                    oauthTokenSecret = qs["oauth_token_secret"];
                    string endpoint   = qs["endpoint"];
                    string revokecode = CreateToken(oauthTokenSecret + "&" + init.gearsecret, oauthToken);
                    revokecode                  = revokecode.Replace('/', '_');
                    url                         = client.BuildUri(request).ToString();
                    this.accesstoken.token      = oauthToken;
                    this.accesstoken.secret     = oauthTokenSecret;
                    this.accesstoken.endpoint   = endpoint;
                    this.accesstoken.revokecode = revokecode;
                    this.tokencache.accesstoken = this.accesstoken;
                    this.tokencache.key         = init.gearkey;
                    this.token._                = this.tokencache;
                    this.cache.set_item(this.token, "microgear-" + init.gearkey + ".cache");
                }
                else
                {
                    this.onError("Access token is not issued, please check your consumerkey and consumersecret.");
                    reset = true;
                    this.ResetToken();
                }
            }
            else
            {
                this.onError("Request token is not issued, please check your appkey and appsecret.");
            }
        }