如何将 REST API 与 FireMonkey 结合使用?

BDu*_*rte 3 delphi rest firemonkey

我需要在 FireMonkey 中实现 REST API 来获取一些信息,但我不确定如何做到这一点。

REST API使用OAuth2,我可以访问两个代码:Consumer Key和Consumer Secret。之后,我需要获得一个临时的 Bearer 令牌。

要请求临时令牌,需要向令牌端点https://apigateway.serpro.gov.br/tokenAuthorization执行 HTTP POST 请求,以 Base64 格式在HTTP 标头中告知访问凭证(consumerKey:consumerSecret) , 如下所示。

[POST] grant_type = client_credentials 
[HEAD] Authorization: Basic Base64 (Consumer key: ConsumerSecret)
Run Code Online (Sandbox Code Playgroud)

如何使用 Delphi 获取访问令牌?

小智 5

遗憾的是,当身份验证方法为 client_credentials 时,Delphi REST Framework 附带的 OAuth2 组件无法工作,因此您必须以手动方式进行操作。我已经包含了用于了解其工作原理的测试代码。今天甚至再次测试了它以确保它有效:)

在表单上放置一个备忘录和一个按钮,然后设置 Button1 OnClick 事件,将 <your_xxxx> 更改为适合您的内容,您应该可以开始了(只需检查它是否被命名为 Memo1、Button1 和 Form1)

您也可以使用 Firemonkey 访问 REST 组件,因此这只是为了展示 REST 框架。

unit Unit1;

interface

uses
  Winapi.Windows,
  Winapi.Messages,
  System.SysUtils,
  System.Variants,
  System.Classes,
  Vcl.Graphics,
  Vcl.Controls,
  Vcl.Forms,
  Vcl.Dialogs,
  Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    GaToken: string;
    GaTokenType: string;
    GaTokenExpire: integer;
  end;

var
  Form1: TForm1;

implementation

uses
  REST.Types,
  REST.Client,
  REST.Utils,
  System.NetEncoding,
  System.Net.HttpClient;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  vClientId,
    vClientSecret,
    vScope,
    aExpire: string;
  LClient: TRESTClient;
  LRequest: TRESTRequest;
  LResponse: TRESTResponse;
begin
  // Scope is not mandatory and can be skiped
  vClientId := '<your_clientID>';
  vClientSecret := '<your_seecret>';
  // vScope := '<Your_scope>;

  LClient := TRESTClient.Create('<YOUR_TOKEN_URL>');
  try
    LRequest := TRESTRequest.Create(LClient);
    try
      LResponse := TRESTResponse.Create(LClient);
      try
        LRequest.Client := LClient;  // This line is not really needed but it is good for code readability, see TRESTRequest.Create(LClient);
        LRequest.Response := LResponse;

        LRequest.Method := rmPOST;
        LRequest.AddAuthParameter('grant_type', 'client_credentials', pkGETorPOST);
        LRequest.AddAuthParameter('client_id', vClientId, pkGETorPOST);
        LRequest.AddAuthParameter('client_secret', vClientSecret, pkGETorPOST);
        // LRequest.AddAuthParameter('scope', vScope, pkGETorPOST);

        LRequest.Execute;

        LResponse.GetSimpleValue('access_token', GaToken);
        LResponse.GetSimpleValue('token_type', GaTokenType);
        LResponse.GetSimpleValue('expires_in', aExpire);
        GaTokenExpire := aExpire.ToInteger;

        Memo1.Clear;
        Memo1.Lines.Add(IntToStr(LResponse.StatusCode) + ' / ' + LResponse.StatusText);
        Memo1.Lines.Add('------------   R A W   R E S P O N S E   ---------------------');
        Memo1.Lines.Add(LResponse.Headers.Text);
        Memo1.Lines.Add('--------------------------------------------------------------');
        Memo1.Lines.Add(LResponse.Content);
        Memo1.Lines.Add('--------------------  T O K E N  -----------------------------');
        Memo1.Lines.Add(GAToken);
      finally
        LResponse.Free;
      end;
    finally
      LRequest.Free;
    end;
  finally
    LClient.Free;
  end;
end;

end.
Run Code Online (Sandbox Code Playgroud)