SuiteCRM API完全身份验证和登录

时间:2018-01-09 20:48:49

标签: php rest api suitecrm

通过后台API登录可以获得对SuiteCRM的完全访问权限吗?

例如,我们在一个单独的站点上使用LDAP,并且我想在后台将经过身份验证的用户传递给suitecrm,然后让suitecrm授予对所述用户的访问权限,而无需他们再次登录。

我已经能够让API工作并“登录”用户并返回session_id,但它仍然指示我进入登录页面。如果我试图强制重定向,我会得到一个err_to_many_redirects。

编辑:

我不相信这个问题来自API脚本,而是使用返回的数据来完成所需的会话数据,这是我能够生成的。

以下代码从http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_6.5/Application_Framework/Web_Services/Examples/REST/PHP/Logging_In/

获取
<?php

error_reporting(E_ALL);
ini_set('display_errors',1);

require_once('./shield_secureaccess.php');

$url = "http://{site_location}/service/v4_1/rest.php";
session_start();
$username = $_SESSION['SHIELD_user'];
session_write_close();
if(isset($username)){
        $password = shield_secureaccess($username);}
else{require 'shield_session.php';}

        //function to make cURL request
    function call($method, $parameters, $url)
    {
        ob_start();
        $curl_request = curl_init();

    curl_setopt($curl_request, CURLOPT_URL, $url);
    curl_setopt($curl_request, CURLOPT_POST, 1);
    curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    curl_setopt($curl_request, CURLOPT_HEADER, 1);
    curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);

    $jsonEncodedData = json_encode($parameters);

    $post = array(
         "method" => $method,
         "input_type" => "JSON",
         "response_type" => "JSON",
         "rest_data" => $jsonEncodedData
    );

    curl_setopt($curl_request, CURLOPT_POSTFIELDS, $post);
    $result = curl_exec($curl_request);
    curl_close($curl_request);
        $result = explode("\r\n\r\n", $result, 2);

        $response = json_decode($result[1]);
        ob_end_flush();

        return $response;
    }

    //login ------------------------------
    $login_parameters = array(
         "user_auth" => array(
              "user_name" => $username,
              "password" => md5($password),
              "version" => "1"
         ),
         "application_name" => "RestTest",
         "name_value_list" => array(),
    );

    $login_result = call("login", $login_parameters, $url);



    echo "<pre>";
//      print_r($_SESSION);
    print_r($login_result);
//      print_r($current_user);

print_r($_SESSION);

    echo "</pre>";
    //get session id
    $session_id = $login_result->id;



//      header("Location: http://{site_location}/index.php?module=Home&action=inde");
//      header("Location: http://{site_location}/index.php?MSID=$session_id");
?>

1 个答案:

答案 0 :(得分:0)

你快到了。会话ID无法立即用于登录,您首先必须致电seamless_login

所以流程是:

  1. 如上所述进行API登录。
  2. 调用seamless_login,这将返回0或1
  3. 将用户导航至http://{site_location}/index.php?MSID=$session_id,其中$session_id是原始API登录电话中的会话ID。