使用gocosmos创建文档时未授权

Unauthorized when using gocosmos to create a document

我从 https://github.com/btnguyen2k/gocosmos.

获得了 Azure CosmosDB 的 go-sql- 驱动程序

当我调用 gocosmos.NewRestClient 获取休息客户端、CreateDatabase() 创建数据库和 CreateCollection() 创建集合时一切顺利。

问题是当我使用 CreateDocument() 时,我收到状态代码为 401 的响应,正文如下所示

{"code":"Unauthorized","message":"The input authorization token can't serve the request. Please check that the expected payload is built as per the protocol, and check the key being used. Server used the following payload to sign: 'post\ndocs\ndbs/ToDoList/colls/Items\nmon, 31 may 2021 13:31:44 gmt\n\n'\r\nActivityId: a9bbd729-3495-400f-9d79-ddec3737aa92, Microsoft.Azure.Documents.Common/2.11.0"}

我已经尝试了所有我见过的解决方案,但我没有解决问题。

我遵循了this tutorial,使用这个示例代码,我可以成功地创建数据库、集合和文档。这是我的检测结果,对你有帮助吗?

// connects to MongoDB
func connect() *mongo.Client {
    mongoDBConnectionString := os.Getenv(mongoDBConnectionStringEnvVarName)
    if mongoDBConnectionString == "" {
        log.Fatal("missing environment variable: ", mongoDBConnectionStringEnvVarName)
    }

    database = os.Getenv(mongoDBDatabaseEnvVarName)
    if database == "" {
        log.Fatal("missing environment variable: ", mongoDBDatabaseEnvVarName)
    }

    collection = os.Getenv(mongoDBCollectionEnvVarName)
    if collection == "" {
        log.Fatal("missing environment variable: ", mongoDBCollectionEnvVarName)
    }

    ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
    defer cancel()

    clientOptions := options.Client().ApplyURI(mongoDBConnectionString).SetDirect(true)
    c, err := mongo.NewClient(clientOptions)

    err = c.Connect(ctx)

    if err != nil {
        log.Fatalf("unable to initialize connection %v", err)
    }
    err = c.Ping(ctx, nil)
    if err != nil {
        log.Fatalf("unable to connect %v", err)
    }
    return c
}

// creates a todo
func create(desc string) {
    c := connect()
    ctx := context.Background()
    defer c.Disconnect(ctx)

    todoCollection := c.Database(database).Collection(collection)
    r, err := todoCollection.InsertOne(ctx, Todo{Description: desc, Status: statusPending})
    if err != nil {
        log.Fatalf("failed to add todo %v", err)
    }
    fmt.Println("added todo", r.InsertedID)
}