Delphi RestRequest未经授权(401)错误

时间:2020-01-28 10:03:16

标签: json rest delphi unauthorized

我想调用REST API。但是我总是收到“未经授权”(401)错误。 你能帮我吗?我该如何解决这个问题?

这是我的代码。我真的很困惑。

有变量

var
  AStatusCode: Integer;
  AJSONString: String;
  AJSONValue: TJSonValue;
  AParameter: TRESTRequestParameter;

var
  RESTClient   : TRESTClient;
  RESTRequest  : TRESTRequest;
  RESTResponse : TRESTResponse;
  JSONMensaje  : TJSONObject;
  JSONResponse : TJSONObject;
  jValue       : TJSONValue;
  Token        : string;
  Result:string;
  body:string;
  LClient:TCustomRESTClient;
  HTTPBasicAuthenticator:THTTPBasicAuthenticator;
  simple:TSimpleAuthenticatorAdapter;
  AError:String;

其余代码在下面。

我将HTTPBasicAuthenticator设置为通过用户名和密码进行身份验证并分配一些属性:

Memo1.Lines.Clear;

RESTClient := TRESTClient.Create(EditBaseURL.Text);
RESTResponse := TRESTResponse.Create(nil);
RESTRequest := TRESTRequest.Create(nil);
// Set the initial properties

RESTClient.Params.Clear;
RESTClient.HandleRedirects := true;
if CheckBoxAuthorize.Checked then
begin
  HTTPBasicAuthenticator := THTTPBasicAuthenticator.Create(EditUserName.Text, EditPassword.Text);
  RESTClient.Authenticator := HTTPBasicAuthenticator;
end;
RESTClient.RaiseExceptionOn500 := false;

RESTRequest.Client := RESTClient;
RESTRequest.Response := RESTResponse;
RESTRequest.SynchronizedEvents := false;


RESTRequest.Method := TRESTRequestMethod.rmPOST;
//RESTRequest.Resource := '/shipping/v1/shipments';
RESTRequest.Resource := EditURL.Text;
RESTRequest.Accept := 'application/json';

AParameter := RESTRequest.Params.AddItem;
AParameter.ContentType := ctAPPLICATION_JSON;
AParameter.name := 'Content-Type';
AParameter.Value := 'application/json';
AParameter.Kind := TRESTRequestParameterKind.pkHTTPHEADER;

//RESTRequest.Params.AddHeader('Content-Type','application/json');
RESTRequest.Params.AddHeader('Authorization','Basic YmVoaW5laDpiZWhpbmUxMjM=');
RESTRequest.Params.AddHeader('ServiceAuthorization','YmlsbGluZ19iX2doYXp2aW46Z2hhenZpbkB0Y2kxMjMNCg==');

最后,我使用try..except块来处理异常和释放资源:

try
  AJSONString := EditBody.Text;
  RESTRequest.Body.Add(AJSONString, ctAPPLICATION_JSON);
  try
    RESTRequest.Execute;
    Memo1.Lines.Add('************JSONText*******************');
    Memo1.Lines.Add(RESTResponse.JSONText);
    //Memo1.Lines.Add('JSONValue  :'+RESTResponse.JSONValue.Value);
    Memo1.Lines.Add('StatusText :'+RESTRequest.Response.StatusText );
    Memo1.Lines.Add('StatusCode :'+IntToStr(RESTRequest.Response.StatusCode ));
    Memo1.Lines.Add('ErrorMesage:'+RESTRequest.Response.ErrorMessage);
    Memo1.Lines.Add('************Content*******************');
    Memo1.Lines.Add(RESTRequest.Response.Content);
    Memo1.Lines.Add('************Headers*******************');
    Memo1.Lines.Add(RESTRequest.Response.Headers.Text);
  except
    on E: Exception do
    begin
      Memo1.Lines.Add('************Error*******************');
      Memo1.Lines.Add('Error : ' +E.Message);
      Memo1.Lines.Add('************Error*******************');
      Memo1.Lines.Add('Error : '+RESTResponse.Content);
    end;
  end;

  // If the StatusCode is 200, then the REST method worked

  AStatusCode := RESTRequest.Response.StatusCode;

  if AStatusCode <> 200 then
    AError := RESTRequest.Response.Content
  else
  begin
    AJSONValue := TJSonObject.ParseJSONValue(RESTRequest.Response.Content);
    Memo1.Lines.Add(AJSONValue.Value);
    AJSONValue.Free;
    AError := '';
  end;
finally

end;

0 个答案:

没有答案