Android Question PHP -> B4A, Post cURL, Error

mw71

Active Member
Licensed User
Longtime User
hi
i have become a working PHP script.

PHP:
function curl_post($url, array $post = NULL, array $options = array())
{
    $defaults = array(
        CURLOPT_POST => 1,
        CURLOPT_HEADER => 0,
        CURLOPT_URL => $url,
        CURLOPT_FRESH_CONNECT => 1,
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_FORBID_REUSE => 1,
        CURLOPT_TIMEOUT => 4,
        CURLOPT_POSTFIELDS => http_build_query($post)
    );


    $ch = curl_init();
    curl_setopt_array($ch, ($options + $defaults));
    if( ! $result = curl_exec($ch))
    {
        trigger_error(curl_error($ch));
    }
    curl_close($ch);
    return $result;
}
PHP:
       $url = "https://......../protocol/openid-connect/token";

       // post

       $post = array(
          "client_id" => "watch",
          "client_secret" => "",
          "username" => $user,
          "scope" => "openid",
          "password" => $passwd,
          "grant_type" => "password"
          );

    $json = curl_post($url,$post);


now i have write in B4A:
B4X:
    Dim mPost As Map
    Dim url_Token As String = "https://........./protocol/openid-connect/token"
                           
    Dim h_Spot_Token As HttpJob

                           
    mPost.Initialize
    mPost.Put("client_id","watch")
    mPost.Put("client_secret","")
    mPost.Put("username",box_User.Text)
    mPost.Put("scope","openid")
    mPost.Put("password",box_Pass.Text)
    mPost.Put("grant_type","password")

    JSONGenerator.Initialize(mPost)
                           
    h_Spot_Token.Initialize("",Me)
    h_Spot_Token.PostString(url_Token,JSONGenerator.ToString)
    h_Spot_Token.GetRequest.SetContentType("application/json;charset=UTF-8")

    Wait For (h_Spot_Token) JobDone(h_Spot_Token As HttpJob)
    If h_Spot_Token.Success = True Then
        Log(h_Spot_Token.GetString)
    Else
        Log(h_Spot_Token.ErrorMessage)
    End If

Unfortunately it do not work
- i test on Android 5 System
- java.net.ConnectException: Failed to connect to ....... :443

Is it a SSL Problem or have i do another wrong thing??
 
Last edited:

Emme Developer

Well-Known Member
Licensed User
Longtime User
As i remember, i had a similar issue with a nativescript app, but i didn't found a reference.. Have you tried on another device, with a more recent android version?
 
Upvote 0

mw71

Active Member
Licensed User
Longtime User
Can not answer this; you did not post the full errormessage

Unfortunately it was the full Error Message that i have become from the HttpJob.

Now i can connect, but become a other Error,
Code:
B4X:
    Dim mPost As Map
    Dim url_Token As String = "https://........./protocol/openid-connect/token"
                           
    Dim h_Spot_Token As HttpJob

    mPost.Initialize
    mPost.Put("client_id","watch")
    mPost.Put("client_secret","")
    mPost.Put("scope","openid")
    mPost.Put("username",box_User.Text)
    mPost.Put("password",box_Pass.Text)
    mPost.Put("grant_type","password")

    JSONGenerator.Initialize(mPost)

                              
    h_Spot_Token.Initialize("",Me)
    h_Spot_Token.PostString(url_Token,JSONGenerator.ToString)
    h_Spot_Token.GetRequest.SetHeader("CURLOPT_POST",True)
    h_Spot_Token.GetRequest.SetHeader("CURLOPT_HEADER", False)
    h_Spot_Token.GetRequest.SetHeader("CURLOPT_FRESH_CONNECT", True)
    h_Spot_Token.GetRequest.SetHeader("CURLOPT_RETURNTRANSFER", True)
    h_Spot_Token.GetRequest.SetHeader("CURLOPT_FORBID_REUSE", True)
    h_Spot_Token.GetRequest.SetHeader("CURLOPT_TIMEOUT",4)
    h_Spot_Token.GetRequest.SetHeader("CURLOPT_POSTFIELDS",JSONGenerator.ToString)
    h_Spot_Token.GetRequest.SetContentType("application/json;charset=UTF-8")
    Wait For (h_Spot_Token) JobDone(h_Spot_Token As HttpJob)
    If h_Spot_Token.Success = True Then
        Log(h_Spot_Token.GetString)
    Else
        Log(h_Spot_Token.ErrorMessage)
    End If

Error (response from the Server): {"error":"invalid_request","error_description":"Missing form parameter: grant_type"}
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…