Exemplo n.º 1
0
        public RedirectResult getToken(String url)
        {
            String code = url.Split('=')[1];
            WebClient wc = new WebClient();
            wc.Encoding = Encoding.UTF8;
            String test = "https://oauth.vk.com/access_token?client_id=" + client_id + "&client_secret=" + client_secret + "&code=" + code + "&redirect_uri=" + redirect_uri;
            String access_token = wc.DownloadString("https://oauth.vk.com/access_token?client_id=" + client_id + "&client_secret=" + client_secret + "&code=" + code);
            JObject obj = JObject.Parse(access_token);
            String VkToken = (string)obj["access_token"];

            DatabaseContext db = new DatabaseContext();

            string login = HttpContext.User.Identity.Name;
            User user = db.Users.Where(u => u.Email == login).FirstOrDefault();
            SocAccount socAcc = new SocAccount(0, VkToken, user.Id);
            db.SocAccounts.Add(socAcc);
            db.SaveChanges();
            /*  if (user != null)
              {
                  db.Entry(socAcc).State = EntityState.Modified;
                  db.SaveChanges();
              }         */
            return RedirectPermanent("~/Tool/SocStudio/");
        }
Exemplo n.º 2
0
        public RedirectResult getToken(String code)
        {
            Console.WriteLine("getTokenMethod");
            WebClient wc = new WebClient();
            wc.Encoding = Encoding.UTF8;
            String access_token = wc.DownloadString("https://graph.facebook.com/oauth/access_token?client_id=" + client_id + "&client_secret=" + client_secret + "&code=" + code + "&redirect_uri=" + redirect_uri);

                string login = HttpContext.User.Identity.Name;
                User user = db.Users.Where(u=>u.Email==login).FirstOrDefault();
                //SocAccount socAcc = db.SocAccounts.Where(u => u.ID_USER == user.Id).FirstOrDefault();
                SocAccount socAcc = new SocAccount();
                socAcc.ID_USER = user.Id;
                socAcc.SOCNET_TYPE = 0;
                socAcc.TOKEN = (access_token.Split('='))[1].Split('&')[0];
                db.SocAccounts.Add(socAcc);
                db.SaveChanges();
            return RedirectPermanent("~/Tool/AccountManage/");
        }
Exemplo n.º 3
0
        public static Newsfeed getNewsfeed(SocAccount sa)
        {
            WebClient wc = new WebClient();
            wc.Encoding = Encoding.UTF8;

            String answer = wc.DownloadString("https://api.vk.com/method/newsfeed.get?access_token=" + sa.TOKEN + "&max_photos=100");
            JObject obj = JObject.Parse(answer);
            JToken jtoken = obj["response"]["items"].First;
            Newsfeed newsfeed = new Newsfeed();
            //newsfeed
            do
            {
                switch (jtoken["type"].ToString())
                {
                    case "wall_photo":
                        WallPhoto wallphoto = new WallPhoto();
                        wallphoto.attach = getPhotoAttachments(jtoken);
                        wallphoto.date = (new DateTime(1970, 1, 1, 0, 0, 0, 0)).AddSeconds(int.Parse(jtoken["date"].ToString()));
                        wallphoto.idFrom = jtoken["source_id"].ToString();
                        newsfeed.feed.Add(wallphoto);
                        break;
                    case "post":
                        Post post = new Post();
                        if (jtoken["attachments"]!=null)
                            post.attach = getAttachments(jtoken);
                        post.date = (new DateTime(1970, 1, 1, 0, 0, 0, 0)).AddSeconds(int.Parse(jtoken["date"].ToString()));
                        post.idFrom = jtoken["source_id"].ToString();
                        post.text = jtoken["text"].ToString();
                        post.id = jtoken["post_id"].ToString();
                        newsfeed.feed.Add(post);
                        break;
                    case "photo":
                        Photo photo = new Photo();
                        photo.idFrom = jtoken["source_id"].ToString();
                        photo.date = (new DateTime(1970, 1, 1, 0, 0, 0, 0)).AddSeconds(int.Parse(jtoken["date"].ToString()));
                        photo.attach = getPhotoAttachments(jtoken);
                        newsfeed.feed.Add(photo);
                        break;
                    case "friend":
                        Friend friend = new Friend();
                        friend.date = (new DateTime(1970, 1, 1, 0, 0, 0, 0)).AddSeconds(int.Parse(jtoken["date"].ToString()));
                        friend.attach = getFriendAttachments(jtoken);
                        friend.idFrom = jtoken["source_id"].ToString();
                        newsfeed.feed.Add(friend);
                        break;
                    default:

                        break;
                }

                jtoken = jtoken.Next;
            }
            while (jtoken != null);

            #region profiles
            jtoken = obj["response"]["profiles"].First;
            do
            {
                NFProfile profile = new NFProfile();
                profile.id = jtoken["uid"].ToString();
                profile.first_name = jtoken["first_name"].ToString();
                profile.last_name = jtoken["last_name"].ToString();
                profile.photo_url = jtoken["photo"].ToString();

                newsfeed.profiles.Add(profile);
                jtoken = jtoken.Next;
            }
            while (jtoken != null);
            #endregion
            #region groups
            jtoken = obj["response"]["groups"].First;
            do
            {
                NFGroup group = new NFGroup();
                group.id = jtoken["gid"].ToString();
                group.name = jtoken["name"].ToString();
                group.screen_name = jtoken["screen_name"].ToString();
                group.photo_url = jtoken["photo"].ToString();

                newsfeed.groups.Add(group);
                jtoken = jtoken.Next;
            }
            while (jtoken != null);
            #endregion
            return newsfeed;
        }