使用Redmine API REST将用户添加到组时,如何解决“用户无效”错误

时间:2019-06-26 07:16:27

标签: php redmine-api

我想使用API​​ REST将用户分配给组。

但这不起作用。

我使用POST /groups/:id/users.:format语法(请参阅Rest Groups

具有此ID的用户也存在于redmine和组中。

在redmine日志中,我可以看到:

Processing by GroupsController#add_users as XML
Parameters: {"group"=>{"user_id"=>"34"},   "key"=>"81aa228c55ac5cfe4264a566ef67ac27702da8eb", "id"=>"5"}
Current user: admin (id=1)
Rendering common/error_messages.api.rsb
Rendered common/error_messages.api.rsb (0.1ms)
Completed 422 Unprocessable Entity in 4ms (Views: 0.4ms |  ActiveRecord: 1.3ms)

在API的响应中:

Code Error :422
Message : User is invalid
In request body : id of user

我将ActiveResouce用于REST API。

$method = 'users'
$options = array('user_id' => user's id to add)

/**
 * Posts to a specified custom method on the current object via:
 *
 *     POST /collection/id/method.xml
 */
function post ($method, $options = array (), $start_tag = false) {
    $req = $this->site . $this->element_name_plural;
    if ($this->_data['id']) {
        $req .= '/' . $this->_data['id'];
    }
    $req .= '/' . $method . '.xml';
    return $this->_send_and_receive ($req, 'POST', $options, $start_tag);
}

此函数用于发送请求并解析响应:

/**
     * Build the request, call _fetch() and parse the results.
     */
    function _send_and_receive ($url, $method, $data = array (), $start_tag = false) {
        $params = '';
        $el = $start_tag ? $start_tag : $this->element_name; // Singular this time
        if ($this->request_format == 'url') {
            foreach ($data as $k => $v) {
                if ($k != 'id' && $k != 'created-at' && $k != 'updated-at') {
                    $params .= '&' . $el . '[' . str_replace ('-', '_', $k) . ']=' . rawurlencode ($v);
                }
            }
            $params = substr ($params, 1);
        } elseif ($this->request_format == 'xml') {
            $params = '<?xml version="1.0" encoding="UTF-8"?><' . $el . ">\n";
            foreach ($data as $k => $v) {
                if ($k != 'id' && $k != 'created-at' && $k != 'updated-at') {
                    $params .= $this->_build_xml ($k, $v);
                }
            }
            $params .= '</' . $el . '>';
        }

        if ($this->extra_params !== false) {
            if(strpos($url, '?'))
            {
                $url = $url .'&'.$this->extra_params;
            }
            else
            {
                $url = $url .'?'.$this->extra_params;
            }

        }

        $this->request_body = $params;
        $this->request_uri = $url;
        $this->request_method = $method;

        $res = $this->_fetch ($url, $method, $params);

        if ($res === false) {
            return $this;
        }

        // Keep splitting off any top headers until we get to the (XML) body:
        while (strpos($res, "HTTP/") === 0) {
            list ($headers, $res) = explode ("\r\n\r\n", $res, 2);
            $this->response_headers = $headers;
            $this->response_body = $res;
            if (preg_match ('/HTTP\/[0-9]\.[0-9] ([0-9]+)/', $headers, $regs)) {
                $this->response_code = $regs[1];
            } else {
                $this->response_code = false;
            }

            if (! $res) {
                return $this;
            } elseif ($res == ' ') {
                $this->error = 'Empty reply';
                return $this;
            }
        }

        // parse XML response
        $xml = new SimpleXMLElement ($res);

        // normalize xml element name in case rails ressource contains an underscore
        if (str_replace ('-', '_', $xml->getName ()) == $this->element_name_plural) {
            // multiple
            $res = array ();
            $cls = get_class ($this);
            foreach ($xml->children () as $child) {
                $obj = new $cls;
                foreach ((array) $child as $k => $v) {
                    $k = str_replace ('-', '_', $k);
                    if (isset ($v['nil']) && $v['nil'] == 'true') {
                        continue;
                    } else {
                        $obj->_data[$k] = $v;
                    }
                }
                $res[] = $obj;
            }
            return $res;
        } elseif ($xml->getName () == 'errors') {
            // parse error message
            $this->error = $xml->error;
            $this->errno = $this->response_code;
            return false;
        }

        foreach ((array) $xml as $k => $v) {
            $k = str_replace ('-', '_', $k);
            if (isset ($v['nil']) && $v['nil'] == 'true') {
                continue;
            } else {
                $this->_data[$k] = $v;
            }
        }

        return $this;
    }

谢谢

0 个答案:

没有答案