• sending token via REST API

    From oksana -@21:1/5 to All on Wed Oct 13 21:31:01 2021
    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
    =================================================

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dan@21:1/5 to All on Thu Oct 14 07:17:10 2021
    Il 14/10/2021 06:31, oksana - ha scritto:
    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
    =================================================

    At a first glance: what is Oerr?
    Dan

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From oksana -@21:1/5 to All on Wed Oct 13 23:34:08 2021
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dan@21:1/5 to All on Thu Oct 14 08:40:16 2021
    Il 14/10/2021 08:34, oksana - ha scritto:
    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

    I mean, you define o and call oErr.
    Dan

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From oksana -@21:1/5 to All on Thu Oct 14 07:06:24 2021
    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



    I mean, you define o and call oErr.
    Dan

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From oksana -@21:1/5 to All on Thu Oct 14 07:01:03 2021
    u can use script

    RECOVER

    MsgStop("ERROR! ")
    RETURN

    END SEQUENCE

    i don't understand about oErr but i want my PRG run very well and the result

    I mean, you define o and call oErr.
    Dan

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dan@21:1/5 to All on Thu Oct 14 18:33:57 2021
    Il 14/10/2021 16:06, oksana - ha scritto:
    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



    I mean, you define o and call oErr.
    Dan

    https://www.access-programmers.co.uk/forums/threads/createobject-msxml2-serverxmlhttp-not-working.312768/

    Why not to use curl?
    Dan

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From oksana -@21:1/5 to All on Thu Oct 14 17:40:28 2021
    https://www.access-programmers.co.uk/forums/threads/createobject-msxml2-serverxmlhttp-not-working.312768/

    Why not to use curl?
    Dan

    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)