Login problem when get issues via rest api

Hello,

We have problem when trying to get issues.

From our developer:

I use Youtrack 3.0. I want to get issues.

For get an issues I must login to Youtrack but I can't.

I sent request to Youtrack and received JSESSIONID and jetbrains.charisma.main.security.PRINCIPAL.

But I can't login to Youtrack.

I tried to do it by the following ways:

1.Set cookies for youtrack.address.com

Code:

setcookie("JSESSIONID","",time()-3600,"/","youtrack.address.com");

setcookie("jetbrains.charisma.main.security.PRINCIPAL","",time()-3600,"youtrack.address.com/");

setcookie("Path","",time()-3600,"/","youtrack.address.com");

setcookie("Expires","",time()-3600,"/","youtrack.address.com");

$JSESSIONID = get_between("JSESSIONID=",";",$header);

$JSESSIONID = $JSESSIONID[0];

$PRINCIPAL = get_between("jetbrains.charisma.main.security.PRINCIPAL=",";",$header);

$PRINCIPAL = $PRINCIPAL[0];

$PATH = "/";

setcookie("JSESSIONID",$JSESSIONID,time()+3600*24,"youtrack.address.com/","youtrack.address.com");

setcookie("jetbrains.charisma.main.security.PRINCIPAL",$PRINCIPAL,time()+3600*24,"youtrack.address.com/","youtrack.address.com");

setcookie("Path","/",time()+3600*24,"youtrack.address.com/","youtrack.address.com");

setcookie("Expires",$Expires,time()+3600*24,"youtrack.address.com/","youtrack.address.com");

---

2.Set cookies via cURL

Code:

  curl_setopt($curl, CURLOPT_URL, 'http://youtrack.address.com/rest/issue/byproject/DF?filter=sort%20by:%20updated&max=3');

  curl_setopt($curl, CURLOPT_HEADER, false);

  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

  if(sizeof($headers) >= 1)

  {

  for ($i=0; $i<sizeof($headers); $i++)

  {

        if (strpos($headers[$i], 'Set-Cookie:') !== FALSE)

        {

                list($field, $cookie[]) = explode(' ', $headers[$i]);

        }

  }

  for ($i=0; $i<sizeof($cookie); $i++)

  {

        curl_setopt($curl, CURLOPT_COOKIE, $cookie[$i]);

  }

  } 

  $data = curl_exec($curl);

---

3.Get issues via fsockopen

Code:

$fp = fsockopen("youtrack.address.com", 80, $errno, $errstr, 10);

$out = "GET http://youtrack.address.com/rest/issue/byproject/DF?filter=sort%20by:%20updated&max=2\n";

$out .= "Connection: keep-alive\n";

$JSESSIONID = get_between("JSESSIONID=",";",$cookie);

$JSESSIONID = $JSESSIONID[0];

$PRINCIPAL = get_between("jetbrains.charisma.main.security.PRINCIPAL=",";",$cookie);

$PRINCIPAL = $PRINCIPAL[0];

$out.="Cookie: \$Version=0; JSESSIONID=$JSESSIONID; \$Path=/; jetbrains.charisma.main.security.PRINCIPAL=$PRINCIPAL; \$Path=/";

fputs($fp, $out);

fclose($fp);

---

But all request return

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><error>You are not logged in.</error>

How can I get an issues?

2 comments
Comment actions Permalink

Hello

you can store cookie and reuse it.

For instance:


$cookiearr = null;
$cookie = null;

function login()
{
    $loginURL = 'http://localhost:8080/rest/user/login';
    $loginData = 'login=root&password=root';


    try {
        $session = curl_init();
        curl_setopt($session, CURLOPT_URL, $loginURL);
        curl_setopt($session, CURLOPT_POST, true);
        curl_setopt($session, CURLOPT_POSTFIELDS, $loginData);
        curl_setopt($session, CURLOPT_HEADER, false);
        curl_setopt($session, CURLOPT_HEADERFUNCTION, 'read_header');
        curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($session);
        curl_close($session);
        print_r($response . "\n");

        return $session;
    }
    catch (Exception $e) {
        tLog($e->getMessage(), 'WARNING');
    }
}

function read_header($session, $string)
{
    global $cookiearr;
    global $cookie;


    $length = strlen($string);

    if (!strncmp($string, "Set-Cookie:", 11)) {
        $cookiestr = trim(substr($string, 11, -1));
        $cookie = explode("\n", $cookiestr);
        $cookie = explode('=', $cookie[0]);
        $cookiename = trim(array_shift($cookie));
        $cookiearr[$cookiename] = trim(implode('=', $cookie));
    }
    $cookie = "";
    if (trim($string) == "") {
        foreach ($cookiearr as $key => $value)
        {
            $cookie .= "$key=$value; ";
        }
    }
    //print_r($cookie);
    //print_r($cookiearr);
    return $length;
}

and how to reuse it:

function getIssue($id)
{
    global $cookie;
    global $session;

    $getIssueURL = 'http://localhost:8080/rest/issue' . '/' . $id;

    $session = curl_init();

    print_r('Get issue by ID: ' . $getIssueURL . "\n");
    curl_setopt($session, CURLOPT_URL, $getIssueURL);
    curl_setopt($session, CURLOPT_POST, false);
    curl_setopt($session, CURLOPT_HEADER, false);
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($session, CURLOPT_COOKIE, $cookie);
    curl_setopt($session, CURLOPT_VERBOSE, false);

    $response = curl_exec($session);
    curl_close($session);

    return $response;
}

0
Comment actions Permalink

Hello.

Thank you for feedback.

We have tried with this code:

$cookiearr = null;

$cookie = null;

$response = null;

$ses = login();

read_header($ses,$response);

echo getIssue(Issue's id);

But that reqest returns "You are not logged in"

print_r($cookie) return:

JSESSIONID=5c9983ltgc181fzbuj992pztc;Path=/; jetbrains.charisma.main.security.PRINCIPAL=ODdlNGUwNmRiMmM3NTU1N2YwYTcyMDQ4YWYzOWY3YzFkOGIwNDE4NWVhNGFiZmNmOWRjNDI1ODQyN2Q1NWVkMjpkZW45NGth;Path=/;Expires=Thu, 23-Aug-2012 11:21:47 GMT;

print_r($cookiearr) return:

Array ( [JSESSIONID] => 1w3x4ji9e2aah1m11ymvsksdr1;Path=/ ) Array ( [JSESSIONID] => 1w3x4ji9e2aah1m11ymvsksdr1;Path=/ ) Array ( [JSESSIONID] => 1w3x4ji9e2aah1m11ymvsksdr1;Path=/ [jetbrains.charisma.main.security.PRINCIPAL] => ODdlNGUwNmRiMmM3NTU1N2YwYTcyMDQ4YWYzOWY3YzFkOGIwNDE4NWVhNGFiZmNmOWRjNDI1ODQyN2Q1NWVkMjpkZW45NGth;Path=/;Expires=Thu, 23-Aug-2012 11:22:56 GMT ) Array ( [JSESSIONID] => 1w3x4ji9e2aah1m11ymvsksdr1;Path=/ [jetbrains.charisma.main.security.PRINCIPAL] => ODdlNGUwNmRiMmM3NTU1N2YwYTcyMDQ4YWYzOWY3YzFkOGIwNDE4NWVhNGFiZmNmOWRjNDI1ODQyN2Q1NWVkMjpkZW45NGth;Path=/;Expires=Thu, 23-Aug-2012 11:22:56 GMT ) Array ( [JSESSIONID] => 1w3x4ji9e2aah1m11ymvsksdr1;Path=/ [jetbrains.charisma.main.security.PRINCIPAL] => ODdlNGUwNmRiMmM3NTU1N2YwYTcyMDQ4YWYzOWY3YzFkOGIwNDE4NWVhNGFiZmNmOWRjNDI1ODQyN2Q1NWVkMjpkZW45NGth;Path=/;Expires=Thu, 23-Aug-2012 11:22:56 GMT )

All code:

function login()

{

    $loginURL = 'http://youtrack.address.com/rest/user/login';

    $loginData = 'login=root&password=root';

global $response;

    try {

        $session = curl_init();

        curl_setopt($session, CURLOPT_URL, $loginURL);

        curl_setopt($session, CURLOPT_POST, true);

        curl_setopt($session, CURLOPT_POSTFIELDS, $loginData);

        curl_setopt($session, CURLOPT_HEADER, false);

        curl_setopt($session, CURLOPT_HEADERFUNCTION, 'read_header');

        curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

        $response = curl_exec($session);

        curl_close($session);

        print_r($response . "\n");

        return $session;

    }

    catch (Exception $e) {

        tLog($e->getMessage(), 'WARNING');

    }

}

function read_header($session, $string)

{

    global $cookiearr;

    global $cookie;

    $length = strlen($string);

    if (!strncmp($string, "Set-Cookie:", 11)) {

        $cookiestr = trim(substr($string, 11, -1));

        $cookie = explode("\n", $cookiestr);

        $cookie = explode('=', $cookie[0]);

        $cookiename = trim(array_shift($cookie));

        $cookiearr[$cookiename] = trim(implode('=', $cookie));

    }

    $cookie = "";

    if (trim($string) == "") {

        foreach ($cookiearr as $key => $value)

        {

            $cookie .= "$key=$value; ";

        }

    }

    //print_r($cookie);

    //print_r($cookiearr);

    return $length;

}

function getIssue($id)

{

    global $cookie;

    global $session;

    $getIssueURL = 'http://youtrack.address.com/rest/issue' . '/' . $id;

    $session = curl_init();

    print_r('Get issue by ID: ' . $getIssueURL . "\n");

    curl_setopt($session, CURLOPT_URL, $getIssueURL);

    curl_setopt($session, CURLOPT_POST, false);

    curl_setopt($session, CURLOPT_HEADER, false);

    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

    curl_setopt($session, CURLOPT_COOKIE, $cookie);

    curl_setopt($session, CURLOPT_VERBOSE, false);

    $response = curl_exec($session);

    curl_close($session);

    return $response;

}

0

Please sign in to leave a comment.