gpt4 book ai didi

json - 如何通过 Delphi 将产品、客户等发布到 WordPress WooCommerce 插件

转载 作者:行者123 更新时间:2023-12-01 23:21:11 32 4
gpt4 key购买 nike

我必须在 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 请求,例如感受我的产品标题、ID 和价格? here is documentation of WooCommerce if it helps谢谢))

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

我尝试发送 {"customers":[{"email":"example@mail.... 如我的 get-request 或 {"email":"example @mail.... 如 WooCommerce 文档中所示。我也尝试在没有数组的情况下发送它 {"customers":{"email":"example@mail.....

我一直收到错误{"code":"json_missing_callback_param","message":"缺少参数数据"}

但是您所需要的只是将“客户”而不是“客户”作为对象以及文档中的所有参数(电子邮件、名字...)发送

这是我的代码,可以工作,我现在可以发布:

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 参数必须作为查询字符串参数添加,并且不包含在授权 header 中。这是因为在 WordPress 中没有可靠的跨平台方法来获取原始请求 header 。

解决方案:

procedure Tfrm_Main.Button4Click(Sender: TObject);var    s: String;begin    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;end;

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

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com