亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 gocosmos 創建文檔時未經授權

使用 gocosmos 創建文檔時未經授權

Go
拉風的咖菲貓 2022-09-05 09:59:45
我從 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"}我已經嘗試了我見過的所有解決方案,但我還沒有解決問題。
查看完整描述

1 回答

?
慕雪6442864

TA貢獻1812條經驗 獲得超5個贊

我按照本教程進行了操作,使用此示例代碼,我可以成功創建數據庫,集合和文檔。這是我的測試結果,它能幫到你嗎?

http://img1.sycdn.imooc.com//6315582c0001e31809870576.jpg

// 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)

}


查看完整回答
反對 回復 2022-09-05
  • 1 回答
  • 0 關注
  • 130 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號