[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: call WebApi requiring API token authorisation
[Thread Prev] | [Thread Next]
- Subject: Re: call WebApi requiring API token authorisation
- From: OlivierCruilles <olivier.cruilles@xxxxxxxx>
- Date: Tue, 20 Jan 2026 15:00:14 -0500
- To: Yahoo via User <user@xxxxxxxxxxxxxxxxxxxxxx>
- Cc: OlivierCruilles <olivier.cruilles@xxxxxxxx>
Hello,
Here a peace of code I wrote in one of my applications. I hope it could help:
You will be able to transmit beaver token by Gambas HttpClient command
-------------------------------------------------------------------------------
Public Function Create_Order(AccessToken As String, app_id As String) As Variant
' Fonction pour Créer une nouvelle commande avec PayPal
Dim hClientPost As HttpClient
Dim sBuffer As String
Dim aHeaderOption As New String[]
Dim aDataJson As New Collection
Dim aDataJsonString As String
Dim TimerStart As Float
Dim TimerStop As Float
Dim TimerDelay As Float
Dim ListOptionUrl As String[]
Dim DataResultPayPalApi As Variant
Dim PurchaseUnit As Collection
Dim Item As Collection
Dim ItemTab As Variant[]
Dim UnitAmount As Collection
Dim Amounts As Collection
Dim ItemTotal As Collection
Dim CurrencyCode As Collection
Dim PurchaseUnitTab As Variant[]
Dim ConvertJson As String
If $UrlPayPalOrder Not Like "{http://,https://}*" Then
Return
Endif
'' Call PayPal API - POST
hClientPost = New HttpClient As "hClientPost"
aHeaderOption = ["Authorization: Bearer " & AccessToken]
' curl -v -X POST "https://api-m.sandbox.paypal.com/v2/checkout/orders/"
' -H 'Content-Type: application/json'
' -H 'Authorization: Bearer ACCESS-TOKEN'
' -d '
' {
' "intent": "CAPTURE",
' "purchase_units": [
' {
' "items": [
' {
' "name": "T-Shirt",
' "description": "Green XL",
' "quantity": "1",
' "unit_amount": {
' "currency_code": "USD",
' "value": "100.00"
' }
' }
' ],
' "amount": {
' "currency_code": "USD",
' "value": "100.00",
' "breakdown": {
' "item_total": {
' "currency_code": "USD",
' "value": "100.00"
' }
' }
' }
' }
' ]
' }'
' Liste des options HTTPClient POST
ListOptionUrl = New String[]
aDataJsonString = JSON.ToString($Purchase.DataOrder)
hClientPost.URL = $UrlPayPalOrder
hClientPost.Async = False
hClientPost.Timeout = 60
hClientPost.Debug = True
hClientPost.UserAgent = "Mozilla/5.0 (platform; rv:gecko-version) Gecko/gecko-trail app-name/app-version"
hClientPost.Post("application/json", aDataJsonString, aHeaderOption)
ModLogs.EcrireLogs("Start API PayPal Order Call - POST...")
ModLogs.EcrireLogs("Begin")
If hClientPost.Status < 0 Then
ModLogs.EcrireLogs("Communication error with PayPal !")
ModLogs.EcrireLogs("Status: " & hClientPost.Status)
Return
Else
' Succès - données lue.
If Lof(hClientPost) Then sBuffer = Read #hClientPost, Lof(hClientPost)
End If
'' Analyse resultat API PayPal
DataResultPayPalApi = JSON.FromString(sBuffer)
ModLogs.EcrireLogs("Buffer: " & sBuffer)
' Try Shell "echo '" & sBuffer & "'" & " | jq ." To ConvertJson
'
' ModLogs.EcrireLogs("\n")
' ModLogs.EcrireLogs("Json Buffer: " & ConvertJson)
TimerStop = Timer
TimerDelay = TimerStop - TimerStart
ModLogs.EcrireLogs("Temps d'execution: " & Format$(TimerDelay, "0.000") & "sec)")
ModLogs.EcrireLogs("Execution time: " & Format$(TimerDelay, "0.000") & "sec)")
If DataResultPayPalApi.Exist("id") Then
Print "Order ID: " & DataResultPayPalApi["id"]
Endif
If DataResultPayPalApi <> Null Then
Return DataResultPayPalApi
End If
Catch
ModLogs.EcrireLogs("Erreur dans CPayPal - Create_Order() - Erreur: " & Error.Text & " " & "Where: " & Error.Where)
End
--------------------------------------------------------------------------------
—
Olivier
> On Tuesday, Jan 20, 2026 at 11:25, Lee <t.lee.davidson@xxxxxxxxx (mailto:t.lee.davidson@xxxxxxxxx)> wrote:
> On 1/19/26 6:16 PM, KKing wrote:
> > call WebApi requiring API token authorisation
> >
> > What's the best way to code calling a WebApi requiring an API "BEARER" token embedded in a header?
> >
> > A quick google initially suggested using Curl but on
> > Debian 12 bookworm
> > Gambas 3.18.0 from the Debian repository for above
> > installed gambas3-gb-net-curl from Debian repository
> > Start new command line project
> > add gb.net.curl component
> > Dim myCurl as Curl
> > etc ...
> >
> > but F8 fails on first line with
> > Class 'Curl' is not creatable (Main:5)
> >
> > On same unit, I can from terminal run Curl command with parameters and all works fine.
> >
> > Is there another component or step to allow this to work in Gambas?
> >
> > or
> >
> > should I be using httpclient now?
> >
> > If the latter is there good example to follow?
> >
> > K
>
> Just use HttpClient. It uses Curl "under the hood".
>
> You can pass custom headers as a string array argument to both the .Get and .Post methods. For example:
> HttpClient.Get(["BEARER=gobbletygook"])
>
>
> --
> Lee
>
> --- Gambas User List Netiquette [https://gambaswiki.org/wiki/doc/netiquette] ----
> --- Gambas User List Archive [https://lists.gambas-basic.org/archive/user] ----
>
>
| call WebApi requiring API token authorisation | KKing <kicking177@xxxxxxxxx> |
| Re: call WebApi requiring API token authorisation | Lee <t.lee.davidson@xxxxxxxxx> |