在delphi中执行用于连接条带支付的curl代码

时间:2015-07-05 11:15:09

标签: delphi http stripe-payments indy stripe-connect

我尝试用delphi将我连接到条带支付测试帐户。

connect 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;

非常感谢任何帮助或建议。 谢谢, 彼得

1 个答案:

答案 0 :(得分:2)

您不得使用表单字段来传输API密钥。而是,设置Request.Username属性。密码为空,因此Request.Passwort未使用。来自您链接的page上的API文档:

  

通过HTTP Basic Auth对API进行身份验证。提供您的API   key作为基本auth用户名。您无需提供密码。

此示例适用于程序文件夹中的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'));