private void ObterToken(Entity tokenConfigRecord) { var client = new HttpClient(); //get all attibutes required to get token string tokenEndPoint = (string)tokenConfigRecord[token_endpoint_attribute]; string clientId = (string)tokenConfigRecord[clientid_attribute]; string clientSecret = (string)tokenConfigRecord[client_secret_attribute]; string resource = (string)tokenConfigRecord[resource_attribute]; client.BaseAddress = new Uri(tokenEndPoint); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded")); var content = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("resource", resource), new KeyValuePair <string, string>("client_id", clientId), new KeyValuePair <string, string>("client_secret", clientSecret), new KeyValuePair <string, string>("client_info", "1"), new KeyValuePair <string, string>("grant_type", "client_credentials") }); var result = client.PostAsync("", content).GetAwaiter().GetResult(); string resultContent = result.Content.ReadAsStringAsync().GetAwaiter().GetResult(); if (!string.IsNullOrEmpty(resultContent) && resultContent.Contains("access_token")) { var tokenParse = new TokenParse(resultContent); Token.Set(ExecutionContext, tokenParse.access_token); var dataExpiracao = DateTime.Now.AddSeconds(tokenParse.expires_in); ExpiresIn.Set(ExecutionContext, dataExpiracao); Sucess.Set(ExecutionContext, true); Message.Set(ExecutionContext, "Sucess"); //grava token na entidade para cache var tokenUpdt = new Entity() { LogicalName = tokenConfigRecord.LogicalName, Id = tokenConfigRecord.Id }; tokenUpdt[data_expiracao] = dataExpiracao; tokenUpdt[valor_token] = tokenParse.access_token; Service.Update(tokenUpdt); } else { if (!string.IsNullOrEmpty(resultContent) && resultContent.Contains("error_description")) { var tokenParse = new TokenParse(resultContent); Message.Set(ExecutionContext, tokenParse.error_description); } else { Message.Set(ExecutionContext, "Requisição não retornou um token válido."); } } }
protected override void OnExecute() { Trace("Start...."); try { //get de record used to configure token creation EntityReference configRefence = TokenConfig.Get(ExecutionContext); if (configRefence == null) { Sucess.Set(ExecutionContext, true); Message.Set(ExecutionContext, "Nenhuma referencia de configuração foi definida na Action."); return; } var tokenConfigRecord = Service.Retrieve(configRefence.LogicalName, configRefence.Id, new ColumnSet(atributos)); ValidarCamposObrigatorios(tokenConfigRecord); var dataExpiracao = tokenConfigRecord.Contains(data_expiracao) ? tokenConfigRecord.GetAttributeValue <DateTime>(data_expiracao) : DateTime.MinValue; //Obtem novo token se atual expirado if (dataExpiracao < DateTime.Now) { ObterToken(tokenConfigRecord); } else { Token.Set(ExecutionContext, tokenConfigRecord.GetAttributeValue <string>(valor_token)); ExpiresIn.Set(ExecutionContext, tokenConfigRecord.GetAttributeValue <DateTime>(data_expiracao)); Sucess.Set(ExecutionContext, true); Message.Set(ExecutionContext, "Sucess"); } } catch (Exception ex) { Message.Set(ExecutionContext, ex.Message); } }