Waqas
Waqas

Reputation: 49

Authorizing Users in couchbase sync gateway

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?

Upvotes: 0

Views: 235

Answers (1)

Coding with Cookie
Coding with Cookie

Reputation: 528

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.

Upvotes: 3

Related Questions