0

通过代码授权用户:

String url = "http://localhost:4984/sync_gateway/_user/GUEST";
    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    con.setRequestMethod("POST");
            con.setRequestProperty("Content-Type", "application/json");


    String urlParameters = "{\"disabled\":\"false\", \"admin_channels\":[\"public\"]}";

    // Send post request
    con.setDoOutput(true);
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.writeBytes(urlParameters);
    wr.flush();
    wr.close();

执行后是代码得到响应代码:405,用户也没有被授权。请告诉我这是授权用户的正确方法吗?

4

1 回答 1

3

以下是我将用户添加到 Couchbase Sync Gateway 的方式:

using ServiceStack;

try
{
CouchbaseSyncUserModel couchUser = new CouchbaseSyncUserModel();
couchUser.admin_channels = new String[] { "public", "some channel name" };
couchUser.email = "some email";
couchUser.password = "a password";
couchUser.name = "users name";

string StatusCode = "";
var respose = "http://localhost:4985/sync_gateway/_user/"
    .PostJsonToUrl(couchUser, responseFilter: response => StatusCode = response.StatusCode.ToString());

if (StatusCode == "Success")
{
    //Code if everything worked correctly
}
else
{
    //Code if Sync Gateway did not add the user
}
}
catch (Exception ex)
{
    //Code when something bad happened
}

型号代码:

class CouchbaseSyncUserModel
{
    public string[] admin_channels { get; set; }
    public string email { get; set; }
    public string name { get; set; }
    public string password { get; set; }
}

用于添加用户的 Couchbase 同步网关文档

在您的代码中,您必须将端口从 更改49844985GUESTString url.

于 2015-06-05T18:17:33.317 回答