sending token via REST API

77 views
Skip to first unread message

oksana -

unread,
Oct 14, 2021, 12:31:02 AMOct 14
to
Hello,

I'm working on converting from php to harbor,
i.e. by sending token via REST API.

After I compiled the PRG file, the results did not come out and stopped at the message "test out-1",
even though in the PHP script it worked.

Can anyone help me to get output for the PRG above ?
thanks

regards
Oksa

PHP ============================================
<?php
//2021-10-14 works!!

$personalToken = "f9v9wwFnusSWbAdt7Yivg58BDeCKm76s";
$url = "https://api.envato.com/v1/market/user:OksaSaja.json";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer ".$personalToken));

$response = @curl_exec($ch);
$responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if (curl_errno($ch) > 0) {
throw new Exception("Failed to connect: " . curl_error($ch));
}

switch ($responseCode) {
case 403: throw new Exception("The personal token is missing the required permission for this script");
case 401: throw new Exception("The personal token is invalid or has been deleted");
}

if ($responseCode !== 200) {
throw new Exception("Got status {$responseCode}, try again shortly");
}

print_r($response);
$body = @json_decode($response);


if ($body === false && json_last_error() !== JSON_ERROR_NONE) {
throw new Exception("Error parsing response, try again");
}

print_r($body);
?>

PRG======================================================
#include "hmg.ch"

Function main
Local cAuth := "f9v9wwFnusSWbAdt7Yivg58BDeCKm76s"
Local cUrl := "https://api.envato.com/v1/market/user:OksaSaja.json"
Local nTimeOut := 20 // seconds
Local oRestApi
Local cReturn, cResponse

oRestApi := Win_OleCreateObject( "MSXML2.ServerXMLHTTP" )
oRestApi:setTimeouts(nTimeout * 1000 /* nResolve */ , nTimeout * 1000 /* nConnect*/ , nTimeout * 1000 /* nSend */, nTimeout * 1000 /* nReceive */ )

If Empty( oRestApi )
? "Error al inicializar..."
Return NIL
Endif

BEGIN SEQUENCE WITH { |o| Break(o) }

oRestApi:Open( "POST", cUrl, .F. )
oRestApi:setRequestHeader( "Content-Type", "application/json" )
oRestApi:setRequestHeader( "Accept", "application/json" )

oRestApi:setRequestHeader( "Authorization: Bearer ", cAuth ) // <<--- maybe error here...

MSGINFO('test out-1')
oRestApi:Send( '' ) // <<--- error here...

MSGINFO('test out-2')
? cReturn := hb_jsonDecode( oRestApi:ResponseText() )

? hb_jsonDecode( oRestApi:ResponseBody() )

RECOVER USING oErr
MSGINFO('test out-3')
cReturn := "Error!" + hb_eol() + If ( ValType( oErr ) = 'O', oErr:Description, oErr )

END SEQUENCE

oRestApi := NIL

Return
=================================================

Dan

unread,
Oct 14, 2021, 1:17:14 AMOct 14
to
At a first glance: what is Oerr?
Dan

oksana -

unread,
Oct 14, 2021, 2:34:09 AMOct 14
to
oErr it is an error object.

The PRG file does not fail at compille, but produces no output. Whereas for the example PHP file it can produce output.

regards
oksa

Dan

unread,
Oct 14, 2021, 2:40:20 AMOct 14
to
I mean, you define o and call oErr.
Dan
Message has been deleted

oksana -

unread,
Oct 14, 2021, 10:06:27 AMOct 14
to
u can use script

RECOVER

MsgStop("ERROR! ")
RETURN

END SEQUENCE

i don't understand about oErr but i want my PRG run very well like my PHP script.

what is your suggestion?

regards
Oksa

Dan

unread,
Oct 14, 2021, 12:34:01 PMOct 14
to

oksana -

unread,
Oct 14, 2021, 8:40:29 PMOct 14
to
I've tried it with curl but it doesn't work

maybe you can try it either with MSXML or CURL with the token and urlapi above. Though the token can run in PHP.

regards
oksa
Reply all
Reply to author
Forward
0 new messages