public string NotificarMLB() { try { var NoticacaoMLB = new StreamReader(Request.InputStream); var NotificacaoRecebida = JsonConvert.DeserializeObject <NotificacaoMLB>(NoticacaoMLB.ReadToEnd()); var Contexto = new MercadoLivreEntities(); var Notificacao = new Notificacao(); Notificacao.Resource = NotificacaoRecebida.resource; Notificacao.UserID = NotificacaoRecebida.user_id; Notificacao.Topic = NotificacaoRecebida.topic; Notificacao.ApplicationID = NotificacaoRecebida.application_id; Notificacao.Attempts = Convert.ToInt32(NotificacaoRecebida.attempts); Notificacao.Sent = NotificacaoRecebida.sent; Notificacao.Received = NotificacaoRecebida.received; Contexto.Notificacao.Add(Notificacao); Contexto.SaveChanges(); return("OK"); } catch (Exception) { return("ERRO"); } }
public UsuarioMLB ObterUsuarioMLB(string UserID) { var ClienteRequest = new RestClient(); var ClienteRest = new RestRequest(); var Contexto = new MercadoLivreEntities(); var UltimoToken = Contexto.Autenticacao.OrderByDescending(x => x.IdToken).FirstOrDefault(); var request = new RestRequest(); if (UserID == null) { request = new RestRequest("https://api.mercadolibre.com/users/me?access_token=" + UltimoToken.AcessToken); } else { request = new RestRequest("https://api.mercadolibre.com/users/" + UserID + "?access_token=" + UltimoToken.AcessToken); } var response = ClienteRequest.Execute(request); if (response.StatusCode.Equals(HttpStatusCode.OK)) { return(JsonConvert.DeserializeObject <UsuarioMLB>(response.Content.ToString())); } throw new Exception(); }
public string GerarToken(string Code) { var Retorno = AutenticarMLB(Code); var Contexto = new MercadoLivreEntities(); var Autenticacao = new Autenticacao(); Autenticacao.AcessToken = Retorno.access_token; Autenticacao.TokenType = Retorno.token_type.ToUpper(); Autenticacao.ExpiresIn = Retorno.expires_in; Autenticacao.Scope = Retorno.scope.ToUpper(); Autenticacao.UserID = Retorno.user_id; Autenticacao.RefreshToken = Retorno.refresh_token; Contexto.Autenticacao.Add(Autenticacao); Contexto.SaveChanges(); return(Autenticacao.AcessToken); }