C sdk upsert authentication error

I am trying to run the Hello Couchbase code on the C SDK’s documentation. I believe it connects to the cluster successfully and open the bucket as well, but while trying to upsert, it gives me an authentication error. I make sure to insert a lot of if statements to see exactly where the problem occurs. What is the best way to go about finding the root cause?

#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;
}

This is the output:

Open bucket: LCB_SUCCESS (0)
Storage callback
Status: LCB_ERR_AUTHENTICATION_FAILURE (206), Key: test-doc, CAS: 0

Are you sure that your username/password are correct and this user has permissions to write data into this bucket?

Yes, the username and password are correct. The user does have permission to write to the bucket. I am able to add documents from the web admin console using the same credentials.

could you export environment variable LCB_LOG_LEVEL=5, re-run your sample and post the log here?

The log is extremely long, should I set LCB_LOGLEVEL=4 or 3 instead?

You can upload it to some pastebin (for instance, https://paste.centos.org/) and post link here.

https://paste.centos.org/view/c8170d80

What is the server version?

Couchbase 4.0.0-4051 Community Edition

According to this page, this version is not supported for about 3 years already
https://www.couchbase.com/support-policy/enterprise-software

Could you try one of the currently supported releases?

@avsej you linked support of enterprise software, the person is using community edition. I think that link is incorrect.

Updated to Couchbase Community 6.6, this is the new error log. It shows a timeout error this time.
https://paste.centos.org/view/3cb44d50

@avsej Is there any update about this that you would recommend? I am stuck on similar issue.