dstotijn / go-notion

Go client for the Notion API.
MIT License
380 stars 39 forks source link

`CreatePage` function always return 400 error #5

Closed mmmommm closed 3 years ago

mmmommm commented 3 years ago

First of all ,thanks for creating the notino-sdk for go.

when the following code execute always return 400 error.

go version go1.15.6 darwin/amd64

func getClient(apiKey string) *notion.Client {
    return notion.NewClient(apiKey)
}

func createPage(c echo.Context) error {
    notionApi := os.Getenv("NOTION_API")
    client    := getClient(notionApi)
    ctx       := context.Background()
    parentId  := "{page_id}"

    titleText := &notion.Text{
        Content: "this is a title of the created page",
    }
    title := []notion.RichText{
        {
            Type: notion.RichTextTypeText,
            Text: titleText,
        },
    }
    params := notion.CreatePageParams{
        ParentType: notion.ParentTypePage,
        ParentID:   parentId,
        Title:      title,
    }

    page, err := client.CreatePage(ctx, params)
    if err != nil {
        fmt.Printf("%s",err)
    }
        fmt.Println(page)
    return nil
}

The following error code will be returned when createPage function is executed.

notion: failed to create page: body failed validation. Fix one: body.parent.type should be not present, instead was `"database_id"`. body.parent.page_id should be defined, instead was `undefined`. (code: validation_error, status: 400)

I think the structure is correct, so I'm thinking there's probably something wrong with the code.

Thank you .

dstotijn commented 3 years ago

What version of the client are you using? Fixed a few things since v0.1.0. I tested your code on v0.1.2, and it works there.

mmmommm commented 3 years ago

go.mod

module github.com/CA-22-engineers/nippou

go 1.15

require (
    github.com/dstotijn/go-notion v0.1.2 // indirect
    github.com/joho/godotenv v1.3.0 // indirect
    github.com/labstack/echo/v4 v4.3.0 // indirect
)

I installed this sdk yesterday, so I think it's up to date.

The following curl command, which I typed in my hand, is failing and returning the same error.

curl -X POST https://api.notion.com/v1/pages \
         -H "Authorization: Bearer XXXX" \
         -H "Content-Type: application/json" \
         -H "Notion-Version: 2021-05-13" \
         --data '{
           "parent": {
             "type": "page_id",
             "page_id": "48f8fee-9cd7-9418-0bc2f-ec0398253067"
           },
           "propaties": {
             "Name": {
               "title": [
                 {
                   "text": {
                     "content": "Yurts in Big Sur, California"
                   } 
                 }
               ]
             }
           }
        }'
dstotijn commented 3 years ago

Ah, I actually fixed this on master but forgot to roll a release. Did it just now: v0.1.3.

mmmommm commented 3 years ago

Thanks ! It seems that there is one place that forgot to fix . my friend make pull request, please check it.