使用gocosmos创建文档时出现未经授权的错误。

huangapple go评论164阅读模式
英文:

Unauthorized when using gocosmos to create a document

问题

我从<https://github.com/btnguyen2k/gocosmos>获取了适用于Azure CosmosDB的go-sql-driver。

当我调用gocosmos.NewRestClient来获取一个rest客户端时,它运行良好,使用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"}

我尝试了所有我看到的解决方案,但问题仍未解决。

英文:

I got go-sql-driver for Azure CosmosDB from <https://github.com/btnguyen2k/gocosmos>.

It goes well when i call gocosmos.NewRestClient to get a rest client, CreateDatabase() to create database and CreateCollection() to create collection.

The problem is when i use CreateDocument(), i get response with statuscode 401 and body like this

{&quot;code&quot;:&quot;Unauthorized&quot;,&quot;message&quot;:&quot;The input authorization token can&#39;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: &#39;post\ndocs\ndbs/ToDoList/colls/Items\nmon, 31 may 2021 13:31:44 gmt\n\n&#39;\r\nActivityId: a9bbd729-3495-400f-9d79-ddec3737aa92, Microsoft.Azure.Documents.Common/2.11.0&quot;}

i've tried all the solutions I've seen, but i haven't solved the problem.

答案1

得分: 0

我按照这个教程进行操作,并使用这个示例代码成功地创建了数据库、集合和文档。这是我的测试结果,它能帮到你吗?

使用gocosmos创建文档时出现未经授权的错误。

// 连接到 MongoDB
func connect() *mongo.Client {
    mongoDBConnectionString := os.Getenv(mongoDBConnectionStringEnvVarName)
    if mongoDBConnectionString == "" {
        log.Fatal("缺少环境变量: ", mongoDBConnectionStringEnvVarName)
    }

    database = os.Getenv(mongoDBDatabaseEnvVarName)
    if database == "" {
        log.Fatal("缺少环境变量: ", mongoDBDatabaseEnvVarName)
    }

    collection = os.Getenv(mongoDBCollectionEnvVarName)
    if collection == "" {
        log.Fatal("缺少环境变量: ", 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("无法初始化连接 %v", err)
    }
    err = c.Ping(ctx, nil)
    if err != nil {
        log.Fatalf("无法连接 %v", err)
    }
    return c
}

// 创建一个待办事项
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("添加待办事项失败 %v", err)
    }
    fmt.Println("已添加待办事项", r.InsertedID)
}
英文:

I followed this tutorial and with this sample code, I can successfully create database, collection and document. Here's my testing result, can it help you?

使用gocosmos创建文档时出现未经授权的错误。

// connects to MongoDB
func connect() *mongo.Client {
mongoDBConnectionString := os.Getenv(mongoDBConnectionStringEnvVarName)
if mongoDBConnectionString == &quot;&quot; {
log.Fatal(&quot;missing environment variable: &quot;, mongoDBConnectionStringEnvVarName)
}
database = os.Getenv(mongoDBDatabaseEnvVarName)
if database == &quot;&quot; {
log.Fatal(&quot;missing environment variable: &quot;, mongoDBDatabaseEnvVarName)
}
collection = os.Getenv(mongoDBCollectionEnvVarName)
if collection == &quot;&quot; {
log.Fatal(&quot;missing environment variable: &quot;, 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(&quot;unable to initialize connection %v&quot;, err)
}
err = c.Ping(ctx, nil)
if err != nil {
log.Fatalf(&quot;unable to connect %v&quot;, 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(&quot;failed to add todo %v&quot;, err)
}
fmt.Println(&quot;added todo&quot;, r.InsertedID)
}

huangapple
  • 本文由 发表于 2021年5月31日 22:15:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/67775438.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定