0

Authorizing User through code:

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();

after executing this is code getting response code : 405 and user is also not authorized. kindly tell me is this a correct way for Authorizing Users?

Waqas
  • 49
  • 4
  • 1
    You are using the wrong port. You need to post to 4985 (the admin port) not 4984 (the public port). – borrrden Jun 12 '15 at 22:41

1 Answers1

3

Here is how I have added users to 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
}

Model Code:

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

In your code you will have to change the port from 4984 to 4985and remove GUEST from the end of your String url.