在 delphi 中执行一个 curl 代码以连接到 stripe payment
Execute in delphi a curl code for connection to stripe payment
我尝试使用 delphi 将我连接到我的条纹支付测试帐户。
连接 API 在这里:
Stripe connect API
卷曲示例:
curl https://api.stripe.com/v1/charges \
-u sk_test_CpkBxhx9gcmNYYQTZIXU43Bv:
我尝试将 Indy TIdHTTP 组件与 TIdSSLIOHandlerSocketOpenSSL 一起使用
并使用 Tstringlist 或 TIdMultipartFormDataStream 作为参数调用 post
但我总是收到回复:401 - 未经授权
这是我的代码:
var
Data: TIdMultipartFormDataStream;
https: TIdHTTP;
ssl: TIdSSLIOHandlerSocketOpenSSL;
begin
https := TIdHTTP.Create(self);
ssl := TIdSSLIOHandlerSocketOpenSSL.Create(Self);
https.IOHandler := ssl;
https.Request.BasicAuthentication := True;
Data := TIdMultipartFormDataStream.Create;
//Data.AddFormField('api_key', 'sk_test_CpkBxhx9gcmNYYQTZIXU43Bv');
Data.AddFormField('apikey', 'sk_test_CpkBxhx9gcmNYYQTZIXU43Bv');
https.Post('https://api.stripe.com/v1/charges', Data);
Memo1.lines.Add( https.ResponseText );
Data.Free;
end;
非常感谢任何帮助或建议。
谢谢,
彼得
您不得使用表单域来传输 API 密钥。相反,设置 Request.Username 属性。密码为空,因此 Request.Passwort 未使用。来自链接 page 上的 API 文档:
Authentication to the API occurs via HTTP Basic Auth. Provide your API
key as the basic auth username. You do not need to provide a password.
此示例适用于程序文件夹中的 Indy 10.6.2 和 OpenSSL 库:
program Project31229779;
{$APPTYPE CONSOLE}
uses
IdHTTP, SysUtils;
var
HTTP: TIdHTTP;
begin
HTTP := TIdHTTP.Create;
try
HTTP.Request.BasicAuthentication := True;
HTTP.Request.Username := 'sk_test_CpkBxhx9gcmNYYQTZIXU43Bv';
try
WriteLn(HTTP.Get('https://api.stripe.com/v1/charges'));
except
on E: EIdHTTPProtocolException do
begin
WriteLn(E.Message);
WriteLn(E.ErrorMessage);
end;
on E: Exception do
begin
WriteLn(E.Message);
end;
end;
finally
HTTP.Free;
end;
ReadLn;
end.
注意:您也可以将用户名/密码放在URL:
HTTP.Request.BasicAuthentication := True;
try
WriteLn(HTTP.Get('https://sk_test_CpkBxhx9gcmNYYQTZIXU43Bv:@api.stripe.com/v1/charges'));
我尝试使用 delphi 将我连接到我的条纹支付测试帐户。
连接 API 在这里: Stripe connect API 卷曲示例:
curl https://api.stripe.com/v1/charges \
-u sk_test_CpkBxhx9gcmNYYQTZIXU43Bv:
我尝试将 Indy TIdHTTP 组件与 TIdSSLIOHandlerSocketOpenSSL 一起使用 并使用 Tstringlist 或 TIdMultipartFormDataStream 作为参数调用 post 但我总是收到回复:401 - 未经授权
这是我的代码:
var
Data: TIdMultipartFormDataStream;
https: TIdHTTP;
ssl: TIdSSLIOHandlerSocketOpenSSL;
begin
https := TIdHTTP.Create(self);
ssl := TIdSSLIOHandlerSocketOpenSSL.Create(Self);
https.IOHandler := ssl;
https.Request.BasicAuthentication := True;
Data := TIdMultipartFormDataStream.Create;
//Data.AddFormField('api_key', 'sk_test_CpkBxhx9gcmNYYQTZIXU43Bv');
Data.AddFormField('apikey', 'sk_test_CpkBxhx9gcmNYYQTZIXU43Bv');
https.Post('https://api.stripe.com/v1/charges', Data);
Memo1.lines.Add( https.ResponseText );
Data.Free;
end;
非常感谢任何帮助或建议。 谢谢, 彼得
您不得使用表单域来传输 API 密钥。相反,设置 Request.Username 属性。密码为空,因此 Request.Passwort 未使用。来自链接 page 上的 API 文档:
Authentication to the API occurs via HTTP Basic Auth. Provide your API key as the basic auth username. You do not need to provide a password.
此示例适用于程序文件夹中的 Indy 10.6.2 和 OpenSSL 库:
program Project31229779;
{$APPTYPE CONSOLE}
uses
IdHTTP, SysUtils;
var
HTTP: TIdHTTP;
begin
HTTP := TIdHTTP.Create;
try
HTTP.Request.BasicAuthentication := True;
HTTP.Request.Username := 'sk_test_CpkBxhx9gcmNYYQTZIXU43Bv';
try
WriteLn(HTTP.Get('https://api.stripe.com/v1/charges'));
except
on E: EIdHTTPProtocolException do
begin
WriteLn(E.Message);
WriteLn(E.ErrorMessage);
end;
on E: Exception do
begin
WriteLn(E.Message);
end;
end;
finally
HTTP.Free;
end;
ReadLn;
end.
注意:您也可以将用户名/密码放在URL:
HTTP.Request.BasicAuthentication := True;
try
WriteLn(HTTP.Get('https://sk_test_CpkBxhx9gcmNYYQTZIXU43Bv:@api.stripe.com/v1/charges'));