public async Task<IEnumerable<IDialog>> GetDialogs(DataIUser user, enSocialNetwork socialNetwork)
		{
			Account acc = Account.Deserialize(user.SerializeInfo);
			var request = new OAuth2Request("GET", new Uri("https://api.vk.com/method/messages.getDialogs"), null, acc);

			request.Parameters.Add("count", "200");
			request.Parameters.Add("v", "5.37");

			var res = await request.GetResponseAsync();
			var responseText = res.GetResponseText();

			var msg = JsonConvert.DeserializeObject<XamarinSocialApp.Droid.Data.VkData.VkDialogsResponse>(responseText);

			IList<IDialog> dialogs = new List<IDialog>();

			foreach (var item in msg.Response.Messages)
			{
				IUser userDialog = new User() { Uid = item.Message.UserId, SerializeInfo = user.SerializeInfo };
				//var userDialog = await GetUserInfoRequest(item.Message.UserId, acc, socialNetwork);
				dialogs.Add(new DataDialogs(userDialog, new List<IMessage>() 
				{ 
					new DataMessage() { Content = item.Message.Body } 
				}));
			}

			return dialogs;
		}
		public async Task<IUser> Login(enSocialNetwork socialNetwork)
		{
			IUser user = null;
			try
			{
				TaskCompletionSource<int> ts = new TaskCompletionSource<int>();

				var auth = new OAuth2Authenticator(
				clientId: "5042701",
				scope: "offline,messages,friends",
				authorizeUrl: new Uri("https://oauth.vk.com/authorize"),
				redirectUrl: new Uri("https://oauth.vk.com/blank.html"));

				auth.AllowCancel = true;

				auth.Completed += (s, ee) =>
				{
					if (!ee.IsAuthenticated)
					{
						ts.SetResult(0);
						return;
					}

					var request = new OAuth2Request("GET", new Uri("https://api.vk.com/method/users.get"), null, ee.Account);
					request.GetResponseAsync().ContinueWith(t =>
					{
						if (t.IsCompleted)
						{
							Token = ee.Account.Properties["access_token"].ToString();
							string account = ee.Account.Serialize();
							Account = ee.Account;

							//AccountStore.Create(Forms.Context).Save(ee.Account, "Vk");
							
							var response = t.Result.GetResponseText();
							var users = JsonConvert.DeserializeObject<XamarinSocialApp.Droid.Data.VkData.VkUsers>(response);

							string uid = users.response[0].uid;
							string firstName = users.response[0].first_name;
							string lastName = users.response[0].last_name;

							user = new User()
							{
								FirstName = firstName,
								LastName = lastName,
								Uid = uid,
								SerializeInfo = Account.Serialize(),
								SocialNetwork = enSocialNetwork.VK
							};

							ts.SetResult(0);
						}
						else
						{
							ts.SetResult(0);
							return;
						}
					}, UIScheduler);
				};

				var intent = auth.GetUI(Forms.Context);
				Forms.Context.StartActivity(intent);
				await ts.Task;
			}
			catch (Exception ex)
			{
			}

			return user;
		}
Esempio n. 3
0
		public async Task<IEnumerable<IDialog>> GetDialogs(IUser user, enSocialNetwork socialNetwork)
		{
			IList<IDialog> dialogs = null;
			try
			{
				String url =
					String.Format("https://api.vk.com/method/messages.getDialogs?&count={0}&access_token={1}"
					, 100.ToString(), user.SerializeInfo);

				HttpClient httpClient = new HttpClient();
				string response =
					await httpClient.GetStringAsync(new Uri(url));

				string[] responseString = response.Split('[');
				string[] responseDialogsArray = responseString[1].Split('{');

				dialogs = new List<IDialog>();
				foreach (var item in responseDialogsArray)
				{
					try
					{
						string uid = item.Split(',')[3].Split(':')[1];
						string message = String.Empty;
						try
						{
							message = item.Split(',')[6].Split(':')[1];
						}
						catch (Exception ex)
						{
							message = item.Split(',')[6].Split('\"')[0].Trim();
						}

						IUser friend = new User() { Uid = uid, SerializeInfo = user.SerializeInfo };
						IMessage friendMessage = new Message() { Content = message };
						IDialog dialog = new Dialog(friend, new List<IMessage>() { friendMessage });
						dialogs.Add(dialog);
					}
					catch (Exception ex)
					{

					}
				}
			}
			catch (Exception ex)
			{

			}

			return dialogs;
		}