2015-06-05 47 views
0

通过代码授权用户授权用户:在couchbase同步网关

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 和用户还没有被授权。 请告诉我这是授权用户的正确方法吗?

+1

您正在使用错误的端口。你需要发布到4985(管理端口)而不是4984(公共端口)。 – borrrden

回答

3

这是我如何添加用户Couchbase的同步网关:

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并从String url的末尾删除GUEST

相关问题