Ir para conteúdo
  • Cadastre-se

dev botao
  • Este tópico foi criado há 640 dias atrás.
  • Talvez seja melhor você criar um NOVO TÓPICO do que postar uma resposta aqui.

Recommended Posts

Postado

Boa Tarde .

Tenho  este código abaixo para conexão Rest para gerar um token , porém necessito através do Rest enviar o certificado na requisição, não achei nenhum exemplo.

No Rest Debugger funciona ele pede o certificado.

Segue abaixo o código:

Desde já agradeço.

function teste(): WideString;
  var
    restClient: TRESTClient;
    restRequest: TRESTRequest;
    restResponse: TRESTResponse;
  begin
    restClient := TRESTClient.Create('');
    restRequest := TRESTRequest.Create(restClient);
    restResponse := TRESTResponse.Create(restClient);

    try
      try
        restClient.Accept := 'application/json, text/plain; q=0.9, text/html;q=0.8,';
        restClient.AcceptCharset := 'UTF-8, *;q=0.8';
        restClient.AutoCreateParams := true;
        restClient.AllowCookies := True;
        restClient.BaseURL := 'https://mtls-mp.hml.flagship.maas.link/auth/realms/Matera/protocol/openid-connect/token';
        restClient.ContentType := 'application/x-www-form-urlencoded';
        restClient.FallbackCharsetEncoding := 'UTF-8';
        restClient.HandleRedirects := True;

        restResponse.ContentType := 'text/xml';

        restRequest.Accept := 'application/json, text/plain; q=0.9, text/html;q=0.8,';
        restRequest.AcceptCharset := 'UTF-8, *;q=0.8';
        restRequest.AcceptEncoding := '';
        restRequest.Client := restClient;
        restRequest.Method := rmPOST;


        restRequest.SynchronizedEvents := False;
        restRequest.Response := restResponse;

        restRequest.Params.Add;
        RESTRequest.Params.Items[0].name  := 'grant_type';
        RESTRequest.Params.Items[0].Value := 'client_credentials';
        restRequest.Params[0].Kind := pkGETorPOST;
        restRequest.Params[0].ContentType := ctNone;

        restRequest.Params.Add;
        RESTRequest.Params.Items[1].name  := 'client_id';
        RESTRequest.Params.Items[1].Value := 'teste-hml';
        restRequest.Params[1].Kind := pkGETorPOST;
        restRequest.Params[1].ContentType := ctNone;

        restRequest.Params.Add;
        RESTRequest.Params.Items[2].name  := 'client_secret';
        RESTRequest.Params.Items[2].Value := '12345678';
        restRequest.Params[2].Kind := pkGETorPOST;
        restRequest.Params[2].ContentType := ctNone;

        restRequest.Execute;
        result := restResponse.Content;
      except
        on e: Exception do
        begin
          result := 'Erro: ' + e.Message + ' XML: ' + restResponse.Content;
        end
      end;
    finally
      restClient.Free;
    end;
  end;

  • Solution
Postado

Resolvido através do HTTP

Segue código abaixo

function ObterToken():integer;
var
  HTTPClient: TIdHTTP;
  SSLIOHandler: TIdSSLIOHandlerSocketOpenSSL;
  url,json: string;
  JsonStreamRetorno, JsonStreamEnvio: TStringStream;
  retorno : integer;
begin
  url := 'https://mtls-mp.hml.flagship.maas.link/auth/realms/Matera/protocol/openid-connect/token';

  HTTPClient := TIdHTTP.Create(nil);
  HTTPClient.Request.CharSet := 'utf-8';
  HTTPClient.Request.Accept := '*/*';
  HTTPClient.Request.AcceptEncoding := 'gzip, deflate, br';
  HTTPClient.Request.CustomHeaders.Values['Content-Type'] := 'application/x-www-form-urlencoded';
  HTTPClient.HTTPOptions := [hoKeepOrigProtocol, hoInProcessAuth];
  HTTPClient.HandleRedirects := True;

  json:= 'grant_type=client_credentials&client_id=teste&client_secret=123456789'; //aqui era o maior problema 

  JsonStreamEnvio   := TStringStream.Create(utf8Encode(json));

  JsonStreamRetorno := TStringStream.Create('');

  SSLIOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
  try
    SSLIOHandler.SSLOptions.Mode := sslmUnassigned;
    SSLIOHandler.SSLOptions.CertFile := 'd:\matera\novo\certificate.pem';
    SSLIOHandler.SSLOptions.KeyFile := 'd:\matera\novo\certificate.key';
    SSLIOHandler.SSLOptions.SSLVersions := [sslvTLSv1,sslvTLSv1_1,sslvTLSv1_2];
    HTTPClient.IOHandler := SSLIOHandler;

    try
      HTTPClient.Post(url, JsonStreamEnvio, JsonStreamRetorno);
    except
      retorno := HTTPClient.ResponseCode;
    end;

    retorno := HTTPClient.ResponseCode;
    if retorno = 200 then
    begin
      JsonStreamRetorno.SaveToFile('d:\token.json');

    end;
  finally
    HTTPClient.Free;
    SSLIOHandler.Free;
  end;
end;

Obrigado Pessoal.

  • Este tópico foi criado há 640 dias atrás.
  • Talvez seja melhor você criar um NOVO TÓPICO do que postar uma resposta aqui.
Visitante
Este tópico está agora fechado para novas respostas
×
×
  • Criar Novo...

Informação Importante

Colocamos cookies em seu dispositivo para ajudar a tornar este site melhor. Você pode ajustar suas configurações de cookies, caso contrário, assumiremos que você está bem para continuar.

The popup will be closed in 10 segundos...
The popup will be closed in 10 segundos...