1

I am implementing a client to consume vtiger REST API and in the login process I manage to get it working with curl but not with Guzzle.

Guzzle code:

$postData = [
    'operation' => 'login',
    'username' => $userName,
    'accessKey' => $generatedKey
];

$response = $client->post($url, [
    'form_params' => $postData
]);

There is not actual Guzzle error or exception but is just that I am not being able to authenticate:

{"success":false,"error":{"code":"INVALID_AUTH_TOKEN","message":"Specified token is invalid or expired"}}

Curl version:

$curl = curl_init($service_url);
$curl_post_data = array(
    'operation' => 'login',
    'username' => $crm_username,
    'accessKey' => md5($crm_token.$crm_useraccesskey),
);

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);

I prefer to use Guzzle but right now I have no clue why it dos not work in Guzzle but it does using curl. Any ideas?

  • Please, provide more information about the resulting error. What error do you get with Guzzle version from the server? – Alexey Shokov Aug 25 '16 at 14:37
  • @AlexeyShockov there is not actual Guzzle error but just I am not getting the expected response despite sending the correct data – DanielRestrepo Aug 26 '16 at 13:21

3 Answers3

1
 $response = $client->request('POST','',['form_params' => ['operation'=>'getchallenge', 'username'=>$userName]  ] ) 

The above won't work, but return crm_token and

$response = $client->request('GET','',['query' => ['operation'=>'getchallenge', 'username'=>$userName] ] )

works fine.

Juan Serrats
  • 1,358
  • 5
  • 24
  • 30
0

Is $generatedKey in the first code is the same as md5($crm_token.$crm_useraccesskey) in the second code ? if No, then correct it and it probably works. If not, based on documentation for Guzzle 6.0, for post requests , you can do as following:

$postData = [
    'operation' => 'login',
    'username' => $userName,
    'accessKey' => $generatedKey
];

$response = $client->request('POST',$url, [
    'form_params' => $postData
]);

for more information, see this: http://docs.guzzlephp.org/en/latest/request-options.html#form-params

vahob
  • 81
  • 1
  • 3
0

Really late to the party but hopefully this will help someone else who has the same problem.

This works fine for me using Guzzle 6.0

use GuzzleHttp\Client;

// vTiger API constants
define('VT_URL', 'http://yoursite.com/webservice.php');
define('VT_USERNAME', 'the_name_of_the_user');
define('VT_ACCESSKEY', 'your_accesskey');

$client = new Client(); //GuzzleHttp\Client

// perform API GET request
$reponse = $client->request('GET', VT_URL, [
    'query' => [
        'operation' => 'getchallenge',
        'username' => VT_USERNAME
    ]
]);

// decode the response
$challenge = json_decode($reponse->getBody());

// If challenge failed
if($reponse->getStatusCode() !== 200 || !$challenge->success) {
    die('getchallenge failed: ' . $challenge['error']['errorMessage']);
}

// Everything ok so create a token from response
$token = $challenge->result->token;

// Create unique key using combination of challengetoken and accesskey
$generatedkey = md5($token . VT_ACCESSKEY);

// login using username and accesskey
$reponse = $client->request('POST', VT_URL, [
    'form_params' => [
        'operation' => 'login', 
        'username' => VT_USERNAME, 
        'accessKey' => $generatedkey
    ]
]);

// decode the response
$login_result = json_decode($reponse->getBody()->getContents());

// If api login failed
if($reponse->getStatusCode() !== 200 || !$login_result->success) {
    die('login failed: ' . $login_success['error']['errorMsg']);
}

$sessionid =  $login_result->result->sessionName;

Then you can use the $sessionid to perform other queries

AdRock
  • 2,959
  • 10
  • 66
  • 106