Post

2 followers Follow
0
Avatar

Invalid username when logging in via the REST API

Hi,

 

Trying to login via the API using the following Curl command from POSTMAN...

curl -X POST -H "Content-Type: application/json" -H "Authorization: Basic amFtaWUubG93ZUB3b3JrYm9va3MuY29tOkZlcnJhcmkxMjMh" -H "Cache-Control: no-cache" -H "Postman-Token: ddf6ce13-68b9-c60a-b963-fe984239cde5" "https://api2.clarizen.com/v2.0/services/authentication/login"

It looks as though, it's set to send in JSON format. But I get the message "Invalid username" with a referenceID of "T5Jab3slBXnuKz0K89Dds" I'm using the same credentials that I use via the desktop - should I be using a different set of credentials or an API key? It doesn't look like I need to from the API docs.

Any ideas whats going wrong please?

Jamie Lowe Answered

Please sign in to leave a comment.

8 comments

0
Avatar

This seems a tad too complex for this forum - I suggest you contact a Clarizen expert.

Guy 0 votes
0
Avatar

Hi Jamie,

The authorization header should contain: "Session " + your session id. You have "Basic " instead.

Please check the REST API documentation for more information.

Hope this helps,

Ophir

Ophir Kenig 0 votes
0
Avatar

Hi Ophir,

Thanks for your response - I'm not sure I understand.

I've made a call to authentication/getServerDefinition to retrieve the API application url.

Isn't the next call to login, which is the one that retrieves my Session ID for future calls? Or have  I misunderstood something?

At that point, I would only have the username and password...

Jamie Lowe 0 votes
0
Avatar

Hi Jamie,

Sorry, I didn't read your message properly.

I believe you should be calling login WITHOUT the Authorization header using the Clarizen credentials in the POST body (I suppose you deliberately removed the body from your previous message. Anyway, the login command should look similar to:

Curl https://api2.clarizen.com/v2.0/services/authentication/login -X POST -d {"UserName":"My user name","Password":"My password"}

).

This should be responded with a session id that is to be used in further calls to REST API. The session Id should be placed in the authorization header. 

Please check the REST API documentation for more information.

Hope this helps,

Ophir

Ophir Kenig 0 votes
0
Avatar

Hi Jamie,

If you're using POSTMAN, don't use the predefined authorization headers. Add your own according to the Clarizen documentation, see screenshots below:

 

 

Once you got the session id after logging in, add the authorization header:

Hope this helps,

Ophir

Ophir Kenig 0 votes
0
Avatar

Hi Ophir,

Not sure why but I was still getting the same error back so I've written a quick PHP script to try and login instead:

 

<?php
/*
*
*
*
* Script Author: Jamie Lowe
* Script Version: v1.0
* Script Completion Date: 27/10/2016
*
*/

class Clarizen {

public $server_location;

function __construct() {
global $workbooks, $params;
$this->workbooks = $workbooks;
$this->params = $params;
date_default_timezone_set('Europe/London');
}

//CURL request
public function curl_request($url, $params=[], $request_type = 'get', $access_token, $base_url = 'https://api.clarizen.com/v2.0/services/') {
$ch = curl_init();

$headers = [
"Content-Type: application/json",
//"Authorization: bearer " .$access_token
];
$opts = [
CURLOPT_URL => $base_url.$url,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_0 ,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => 0,
];
if ($request_type == 'post') {
$opts[CURLOPT_POST] = 1;
$opts[CURLOPT_POSTFIELDS] = json_encode($params);
}
if ($request_type == 'patch') {
$opts[CURLOPT_CUSTOMREQUEST] = "PATCH";
$opts[CURLOPT_POSTFIELDS] = json_encode($params);
}
$this->workbooks->log('Curl Opts', $opts);
curl_setopt_array($ch, $opts);
$result = curl_exec($ch);

if ($result === false) {
curl_close($ch);
throw new Exception(curl_error($ch));
}
curl_close($ch);
return $result;
}

public function create_clarizen_session() {
$server_details = $this->curl_request('authentication/getServerDefinition', $params=[], $request_type = 'post', NULL);
//$this->workbooks->log('Server Details', json_decode($server_details, true));
$base = json_decode($server_details, true);
$params = ['userName' => 'XXX', 'password' => 'XXX'];
$session = $this->curl_request('/authentication/login', $params, 'post', NULL, $base['serverLocation']);
//$this->workbooks->log('Session Details', json_decode($session, true));
}

public function run() {
$this->create_clarizen_session();
}

}
$clarizen = new Clarizen();
$clarizen->run();
?>

I'm getting back similar error to what I was before - "Username and Password do not match" - I am using the same credentials as I do via the desktop. I'm obviously still missing something here?

Jamie Lowe 0 votes
0
Avatar

Hi Jamie,

Make sure you provide the user name and password in the call to getServerDefinition, the same way you provide them for the login call.

The user name is used to determine which data center applies (returned in the server location). 
Your organization may be using the European data center which has a different URL.

Please check the REST API documentation for more information.

Hope this helps,

Ophir

Ophir Kenig 0 votes