none
UCWA unsupported_grant_type error

    Question

  • I'm going to integrate skype into a website using skype api for business to have abbility to read and send messages with Skype-for-Business-UCWA-PHP

    this lib -> https://github.com/wapacro/Skype-for-Business-UCWA-PHP

    But I got an error - unsupported_grant_type.

    I've been trying to make it work for 3 day.
    I even started it over from the scratch using source code of this lib.

    Here's the code. Yes, yes.. it's plain and ugly))
    But I'd like to make it work first, OOP is the secondary)

    <?php
    $ucwa_autodiscover = "http://lyncdiscover.MYDOMAIN.onmicrosoft.com/";
    
    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_HEADER => false,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_SSL_VERIFYHOST => false,
        CURLOPT_URL => $ucwa_autodiscover,
        CURLOPT_HTTPHEADER => array(
            "Accept: application/json",
        ),
        CURLOPT_TIMEOUT => 15,
    ));
    
    $response = curl_exec($curl);
    var_dump($response);
    echo "<hr>";
    //die();
    $status = curl_getinfo($curl);
    curl_close($curl);
    
    $login = "LIOGIN@MYDOMAIN.onmicrosoft.com";
    $pass  = "PASSWORD";
    
    //var_dump(json_decode($response, true));
    //die();
    
    if ($status["http_code"] == 200) {
    
        /////////////// AUTODISCOVER ///////////////
    
        $data = json_decode($response, true);
    
        if(array_key_exists('user', $data["_links"])){
            $link_usr = parse_url($data["_links"]["user"]["href"]);
            $link_frm = parse_url($data["_links"]["xframe"]["href"]);
    
            $link_usr["scheme"] = "https";
    
            $ucwa_baseserver = $link_usr["scheme"] . "://" . (substr($link_usr["host"], -1) == "/" ? substr($link_usr["host"], 0, -1) : $link_usr["host"]);
            $ucwa_path_user = ( substr($link_usr["path"], 0, 1) == "/" ? "" : "/" ) . $link_usr["path"] . "?" . $link_usr["query"];
            $ucwa_path_xframe = ( substr($link_frm["path"], 0, 1) == "/" ? "" : "/" ) . $link_frm["path"];
        } else {
    
            echo "NO user in data response, keep trying...<br>";
    
            $curl = curl_init();
            curl_setopt_array($curl, array(
                CURLOPT_HEADER => false,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_SSL_VERIFYPEER => false,
                CURLOPT_SSL_VERIFYHOST => false,
                CURLOPT_URL => $data["_links"]["redirect"]["href"],
                CURLOPT_HTTPHEADER => array(
                    "Accept: application/json",
                ),
                CURLOPT_TIMEOUT => 15,
            ));
    
            $response = curl_exec($curl);
            var_dump($response);
            echo "<hr>";
    
            $status = curl_getinfo($curl);
            curl_close($curl);
    
            $data = json_decode($response, true);
    
            $link_usr = parse_url($data["_links"]["user"]["href"]);
            $link_frm = parse_url($data["_links"]["xframe"]["href"]);
    
            $link_usr["scheme"] = "https";
    
            $ucwa_baseserver = $link_usr["scheme"] . "://" . (substr($link_usr["host"], -1) == "/" ? substr($link_usr["host"], 0, -1) : $link_usr["host"]);
            $ucwa_path_user = ( substr($link_usr["path"], 0, 1) == "/" ? "" : "/" ) . $link_usr["path"] . "?" . $link_usr["query"];
            $ucwa_path_xframe = ( substr($link_frm["path"], 0, 1) == "/" ? "" : "/" ) . $link_frm["path"];
        }
    
        echo "<hr>Autoscover data received:<br>";
        echo "ucwa_baseserver: ".$ucwa_baseserver."<br>";
        echo "ucwa_path_user: ".$ucwa_path_user."<br>";
        echo "ucwa_path_xframe: ".$ucwa_path_xframe."<br>";
    
        /////////////// OAUTH ///////////////
    
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_HEADER => true,
            CURLOPT_NOBODY => true,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => false,
            CURLOPT_URL => $ucwa_baseserver . $ucwa_path_user,
            CURLOPT_REFERER => $ucwa_baseserver . $ucwa_path_xframe,
            CURLOPT_HTTPHEADER => array(
                "Accept: application/json",
            ),
            CURLOPT_TIMEOUT => 15,
        ));
    
        $response = curl_exec($curl);
    
        echo "GETTING Oauth link<br>";
        var_dump($response);
    
        $status = curl_getinfo($curl);
        curl_close($curl);
    
        if ($status["http_code"] == 401) {
            preg_match('/href=["\']?([^"\'>]+)["\']?/', $response, $match);
            $link = parse_url( $match[1] );
    
            $ucwa_path_oauth = (substr($link["path"], 0, 1) == "/" ? "" : "/") . $link["path"];
    
            echo "<hr>";
            echo "Oauth sublink: ".$ucwa_path_oauth;
            echo "<hr>";
    
        } else {
            echo "Can't get OAuth-Link.";
        }
    
        /////////////// TOKEN ///////////////
    
        echo "<hr>getting access token...<hr>";
    
        var_dump($login);
        var_dump($pass);
    
        echo "<br>";
        echo "URL FOR TOKEN: ".$ucwa_baseserver . $ucwa_path_oauth."<br>";
    
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_HEADER => true,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => false,
            CURLOPT_URL => $ucwa_baseserver . $ucwa_path_oauth,
            CURLOPT_REFERER => $ucwa_baseserver . $ucwa_path_xframe,
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => array(
                "grant_type" => "=password&username={$login}&password={$pass}",
                "username" => $login,
                "password" => $pass
            ),
            CURLOPT_HTTPHEADER => array(
                "Accept: application/json"
            ),
            CURLOPT_TIMEOUT => 15,
        ));
    
        $response = curl_exec($curl);
        $status = curl_getinfo($curl);
        curl_close($curl);
    
        echo "<hr>res:";
        var_dump($response);
        echo "<hr>";
    
        /*
        if ($status["http_code"] == 200) {
            $data = json_decode($response, true);
            self::$ucwa_accesstoken = $data["access_token"];
            self::$ucwa_user = $username;
            self::$ucwa_pass = $password;
    
            // Get application link
            return self::getApplicationLink();
        } else {
            echo "Can't get an access token for Skype UCWA";
            return false;
        }
        */
    
    
    }
    
    ?>

    In the result I receive error {"error":"unsupported_grant_type"}".
    And I have no idea what to do with this. 

    I'd be very appreciated if someone helped me out.

    !almost forgot: I test on my local machine with xampp!

    Sunday, August 27, 2017 4:41 AM

All replies

  • I am stuck with the same issue. Did you find a solution ?
    Tuesday, August 29, 2017 1:48 PM
  • Привет. Запарился биться с этой проблемой. Алексей, если ты тут, что-то получилось у тебя?
    Thursday, May 23, 2019 9:26 AM