我必须在Delphi上编写客户端,该客户端应将产品,客户等添加到WP WooCommerce。如果我执行GET请求,我将从WP中获取一些数据。我必须发送该json。
这是我的GET请求返回json string

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, System.JSON, IPPeerClient, System.Rtti,
  System.Bindings.Outputs, Vcl.Bind.Editors, Data.Bind.EngExt,
  Vcl.Bind.DBEngExt, Vcl.StdCtrls, Data.Bind.Components, REST.Client,
  Data.Bind.ObjectScope, REST.Authenticator.OAuth, REST.Authenticator.Basic,
  REST.Authenticator.Simple, REST.Types, REST.JSON, IdHMACSHA1, EncdDecd, HTTPApp,
  IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    RESTClient1: TRESTClient;
    RESTRequest1: TRESTRequest;
    RESTResponse1: TRESTResponse;
    HTTPBasicAuthenticator1: THTTPBasicAuthenticator;
    Button2: TButton;
    Button3: TButton;
    Memo2: TMemo;
    Edit1: TEdit;
    IdHTTP1: TIdHTTP;
    Memo3: TMemo;
    Button4: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  jValue: TJSONValue;
begin

  HTTPBasicAuthenticator1.Username :=
    'ck_6f28f594097d674abf96ebb6b6856ab18b9*****';
  HTTPBasicAuthenticator1.Password :=
    'cs_1eede52f328af091dca5d6c7e0b6a0abc55*****';

  RESTClient1.Params.Clear;

  RESTClient1.AddParameter('consumer_key', HTTPBasicAuthenticator1.Username, TRESTRequestParameterKind.pkGETorPOST);
  RESTClient1.AddParameter('consumer_secret', HTTPBasicAuthenticator1.Password, TRESTRequestParameterKind.pkGETorPOST);
  RESTRequest1.Resource := 'products';
  RESTRequest1.Execute;
  jValue := RESTResponse1.JSONValue;
  Memo1.Lines.Add(jValue.ToString);
end;


有人可以解释或显示我应该如何发送POST请求以及在哪种数据类型中发送,以感受我的产品标题,编号和价格吗?
here is documentation of WooCommerce if it helps
谢谢 ))

所以我解决了我的问题))

我尝试像在get-request中一样发送{"customers":[{"email":"example@mail....,或者像WooCommerce文档中那样发送{"email":"example@mail....。我也尝试发送不带数组的{"customers":{"email":"example@mail....

一直有我的错误{"code":"json_missing_callback_param","message":"Missing parameter data"}

但是,您只需要发送“客户”而不是“客户”作为对象以及文档中的所有参数(电子邮件,first_name ....)即可。

所以这是我的代码,现在可以发布了:

unit Unit1;

interface

uses
   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
   System.Classes, Vcl.Graphics,
   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, System.JSON, IPPeerClient, System.Rtti,
   System.Bindings.Outputs, Vcl.Bind.Editors, Data.Bind.EngExt,
   Vcl.Bind.DBEngExt, Vcl.StdCtrls, Data.Bind.Components, REST.Client,
   Data.Bind.ObjectScope, REST.Authenticator.OAuth, REST.Authenticator.Basic,
   REST.Authenticator.Simple, REST.Types, REST.JSON, IdHMACSHA1, EncdDecd, HTTPApp,
   IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP;

type
  TForm1 = class(TForm)
  Button1: TButton;
  Memo1: TMemo;
  RESTClient1: TRESTClient;
  RESTRequest1: TRESTRequest;
  RESTResponse1: TRESTResponse;
  HTTPBasicAuthenticator1: THTTPBasicAuthenticator;
  Button2: TButton;
  Button3: TButton;
  Memo2: TMemo;
  Edit1: TEdit;
  IdHTTP1: TIdHTTP;
  Memo3: TMemo;
  Button4: TButton;
  procedure Button1Click(Sender: TObject);
  procedure Button2Click(Sender: TObject);
  procedure Button3Click(Sender: TObject);
  procedure Button4Click(Sender: TObject);
private
  { Private-Deklarationen }
public
  { Public-Deklarationen }
end;

var
  Form1: TForm1;

  implementation

  {$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  jValue: TJSONValue;
begin

  HTTPBasicAuthenticator1.Username :=
'ck_6f28f594097d674abf96ebb6b6856ab18b9*****';
  HTTPBasicAuthenticator1.Password :=
'cs_1eede52f328af091dca5d6c7e0b6a0abc55*****';

  RESTClient1.Params.Clear;

  RESTRequest1.Resource := 'customers';
  RESTRequest1.ResourceSuffix :=
'?consumer_key={consumer_key}&consumer_secret={consumer_secret}';
  RESTRequest1.AddParameter('consumer_key', HTTPBasicAuthenticator1.Username,
TRESTRequestParameterKind.pkURLSEGMENT);
  RESTRequest1.AddParameter('consumer_secret', HTTPBasicAuthenticator1.Password,
TRESTRequestParameterKind.pkURLSEGMENT);
  RESTRequest1.AddBody(a.ToJSON, ctAPPLICATION_JSON);
  RESTRequest1.Execute;
  jValue := RESTResponse1.JSONValue;
  Memo1.Lines.Add(jValue.ToString);
end;

最佳答案

一些编码器报告oAuth1存在问题,原因是:

OAuth参数必须作为查询字符串参数添加,并且不包含在Authorization标头中。这是因为没有可靠的跨平台方法来获取WordPress中的原始请求标头。



解:

过程Tfrm_Main.Button4Click(Sender:TObject);
变种
s:字符串;
开始

RESTRequest.ResetToDefaults;
RESTClient.ResetToDefaults;
RESTResponse.ResetToDefaults;
RESTResponseDataSetAdapter.ResetToDefaults;
OAuth1_FitBit.ResetToDefaults;

OAuth1_FitBit.ConsumerKey:= edt_FitBit_ConsumerKey.Text;
OAuth1_FitBit.ConsumerSecret:= edt_FitBit_ConsumerSecret.Text;

RESTClient.BaseURL:= edt_FitBit_BaseURL.Text;

RESTRequest.Resource:= edt_FitBit_ResourceURI.Text;

RESTRequest.AddParameter('oauth_consumer_key',edt_FitBit_ConsumerKey.Text);
RESTRequest.AddParameter('oauth_signature_method','HMAC-SHA1');
RESTRequest.AddParameter('oauth_nonce',OAuth1_FitBit.nonce);
RESTRequest.AddParameter('oauth_timestamp',
OAuth1_FitBit.timeStamp.DeQuotedString);
RESTRequest.AddParameter('oauth_version','1.0');
s:= OAuth1_FitBit.SigningClass.BuildSignature(RESTRequest,OAuth1_FitBit);
RESTRequest.AddParameter('oauth_signature',s);

RESTRequest.Method:= TRESTRequestMethod.rmGET;

RESTRequest.Execute;
结束;

关于json - 如何通过Delphi将产品,客户等发布到WordPress WooCommerce插件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40787897/

10-10 11:34