wapacro / Skype-for-Business-UCWA-PHP

Allows you to use the Skype for Business UCWA in PHP with cURL.
12 stars 9 forks source link

Got unsupported_grant_type error #5

Open swapf opened 7 years ago

swapf commented 7 years ago

Hello. I'm stuck... I've been trying to make it work for the whole day and without success yet. I even started it over from the scratch using source code of this lib. In the result I receive error {"error":"unsupported_grant_type"}". And it seems I have no idea what to with this yet. But I'm sticking it...

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

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

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

<?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;
    }
    */

}

?>
wapacro commented 7 years ago
CURLOPT_POSTFIELDS => array(
            "grant_type" => "=password&username={$login}&password={$pass}",
            "username" => $login,
            "password" => $pass
),

these lines look terribly wrong. Try to stick with the same lines in the source code:

CURLOPT_POSTFIELDS => array(
            "grant_type" => "password",
            "username" => $login,
            "password" => $pass
),