通过代码授权用户:
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 并且用户也未获得授权。 请告诉我这是授权用户的正确方法吗?
答案 0 :(得分: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 Sync Gateway Documentation for Adding Users
在您的代码中,您必须将端口从4984
更改为4985
,并从GUEST
的末尾删除String url
。