Jump to content

Recommended Posts

Posted (edited)

Accessing APIs is new territory for me.  I am trying to access an API using TNetHttpClient component.  The curl example provided is:

curl --request GET 'https://api.samsara.com/fleet/vehicles/stats' \

-d types='engineStates' \

-d decorations='gps' \

--header 'Authorization: Bearer TOKEN' \

-G

I have been unsuccessful at using various settings of TNetHttpClient to get the authorization.  I have the proper token but everything I try, I get the results "message":"invalid token" followed by the request id.

My latest attempt is below but same results.  Can anyone advice and perhaps provide sample code of how to accomplish this?  Thank you in advance.

 

var
   Params: TStringList;
begin

   Params :=TStringList.Create;
   Params.Add('types = engineStates');
   Params.Add('decorations=gps');
   http1.CustHeaders.Add('Authorization', '<My Token>');  //<My Token replaced with actual token
   memo2.Lines.Text :=http1.Post('https://api.samsara.com/fleet/vehicles/stats', Params).ContentAsString;

end;
 

Edited by B4Him

Share this post


Link to post
Posted (edited)

Don't forget the 'Bearer' bit here:

http1.CustHeaders.Add('Authorization', 'Bearer '+MyToken); 

(as for the HTTP requests, I've been using MSXML2_TLB.pas for a long time (see an example here), and lately WinHTTP directly, but I don't have open-sourced that code yet.)

Edited by stijnsanders

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×