Trying to do CRUD functionality with Couchbase 3.1 C SDK but keep encountering LCB_ERR_AUTHENTICATION_FAILURE (206) errors. I believe connection to bucket and cluster is fine but when trying to do upsert or any similar CRUD functionality, authentication errors come up. Tried to debug the error by seeing what rc is returning but it shows it to be SUCCCESS. What is the best way to solve this issue? My code is below as listed:
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#include <iostream>
#include "libcouchbase/couchbase.h"
static void open_callback(lcb_INSTANCE* instance, lcb_STATUS rc)
{
printf("Open bucket: %s\n", lcb_strerror_short(rc));
if (rc != LCB_SUCCESS) {
lcb_destroy(instance);
fprintf(stderr, "Failed to open bucket: %s\n", lcb_strerror_short(rc));
exit(EXIT_FAILURE);
}
}
static void store_callback(lcb_INSTANCE* instance, int cbtype, const lcb_RESPSTORE* resp)
{
const char* key;
size_t nkey;
uint64_t cas;
lcb_respstore_key(resp, &key, &nkey);
lcb_respstore_cas(resp, &cas);
printf("Storage callback\n");
printf("Status: %s, Key: %.*s, CAS: %" PRIx64 "\n",
lcb_strerror_short(lcb_respstore_status(resp)), (int)nkey, key, cas);
}
int main()
{
const char* connection_string = "couchbase://localhost";
const char* username = "username";
const char* password = "password";
const char* bucket = "bucket-name";
//connection credentials
lcb_CREATEOPTS* create_options = NULL;
lcb_createopts_create(&create_options, LCB_TYPE_CLUSTER);
lcb_createopts_connstr(create_options, connection_string, strlen(connection_string));
lcb_createopts_credentials(create_options, username, strlen(username), password, strlen(password));
//create an instance using default values
lcb_INSTANCE* instance;
lcb_STATUS rc = lcb_create(&instance, NULL); //rc is return code that needs to be checked
if (rc != LCB_SUCCESS) {
lcb_destroy(instance);
fprintf(stderr, "Failed to create instance: %s\n", lcb_strerror_short(rc));
exit(EXIT_FAILURE);
}
//initializes with credentials
rc = lcb_create(&instance, create_options);
rc = lcb_createopts_destroy(create_options);
//schedule intial connection to server
rc = lcb_connect(instance);
if (rc != LCB_SUCCESS) {
lcb_destroy(instance);
fprintf(stderr, "Failed to schedule connection object: %s\n", lcb_strerror_short(rc));
exit(EXIT_FAILURE);
}
rc = lcb_wait(instance, LCB_WAIT_DEFAULT);
if (rc != LCB_SUCCESS) {
lcb_destroy(instance);
fprintf(stderr, "Failed to wait for connection: %s\n", lcb_strerror_short(rc));
exit(EXIT_FAILURE);
}
rc = lcb_get_bootstrap_status(instance);
if (rc != LCB_SUCCESS) {
lcb_destroy(instance);
fprintf(stderr, "Failed to bootstrap cluster connection: %s\n", lcb_strerror_short(rc));
exit(EXIT_FAILURE);
}
// associate instance with a bucket
lcb_set_open_callback(instance, open_callback);
rc = lcb_open(instance, bucket, strlen(bucket));
if (rc != LCB_SUCCESS) {
fprintf(stderr, "Failed to schedule open bucket operation: %s\n", lcb_strerror_short(rc));
exit(EXIT_FAILURE);
}
rc = lcb_wait(instance, LCB_WAIT_DEFAULT);
if (rc != LCB_SUCCESS) {
fprintf(stderr, "Failed to wait for open bucket operation: %s\n", lcb_strerror_short(rc));
exit(EXIT_FAILURE);
}
lcb_install_callback(instance, LCB_CALLBACK_STORE, (lcb_RESPCALLBACK)store_callback);
lcb_CMDSTORE* cmd;
const char* key = "test-doc";
const char* value = "{\"Name\": \"name\"}";
rc = lcb_cmdstore_create(&cmd, LCB_STORE_UPSERT); //create upsert command
if (rc != LCB_SUCCESS) {
lcb_destroy(instance);
fprintf(stderr, "Failed to create upsert command: %s\n", lcb_strerror_short(rc));
exit(EXIT_FAILURE);
}
rc = lcb_cmdstore_key(cmd, key, strlen(key)); //assign key for upsert command
if (rc != LCB_SUCCESS) {
lcb_destroy(instance);
fprintf(stderr, "Failed to assign key for upsert: %s\n", lcb_strerror_short(rc));
exit(EXIT_FAILURE);
}
rc = lcb_cmdstore_value(cmd, value, strlen(value)); //assign value for upsert command
if (rc != LCB_SUCCESS) {
lcb_destroy(instance);
fprintf(stderr, "Failed to assign value for upsert: %s\n", lcb_strerror_short(rc));
exit(EXIT_FAILURE);
}
rc = lcb_store(instance, NULL, cmd); //schedule upsert command
if (rc != LCB_SUCCESS) {
lcb_destroy(instance);
fprintf(stderr, "Failed to schedule upsert command: %s\n", lcb_strerror_short(rc));
exit(EXIT_FAILURE);
}
rc = lcb_cmdstore_destroy(cmd); //destroy upsert command
if (rc != LCB_SUCCESS) {
lcb_destroy(instance);
fprintf(stderr, "Failed to wait for upsert to complete: %s\n", lcb_strerror_short(rc));
exit(EXIT_FAILURE);
}
rc = lcb_wait(instance, LCB_WAIT_DEFAULT); //wait for scheduled operations to finish
if (rc != LCB_SUCCESS) {
lcb_destroy(instance);
fprintf(stderr, "Failed to wait for upsert to complete: %s\n", lcb_strerror_short(rc));
exit(EXIT_FAILURE);
}
return 0;
}
Output is this every time:
Open bucket: LCB_SUCCESS (0)
Storage callback
Status: LCB_ERR_AUTHENTICATION_FAILURE (206), Key: test-doc, CAS: 0