A branch to be accessible only by members of a group

Looking at the REST API, I see that when sending this request:

POST /projects/1234/protected_branches

I can specify the access to be limited to groups:

{
    "name": "dev",
    "allowed_to_push": [
    ],
    "allowed_to_merge": [
        {
            "group_id": 12345
        }
    ],
    "allowed_to_unprotect": []
}

First of all, I don’t see the possibility of doing this from the web page. And secondly, when I try to set this access level, I get an error:

{
    "message": [
        "Cannot add users or groups unless they have access to the project"
    ]
}

Based on the error message, I think the problem is that the provided group_id does not have access to the mentioned project which I don’t understand what it means! I tested this with different group_ids. First I tried with the same group’s ID that the project belongs to. Then I tried one level higher, the parent group. And then I used the child group:

Parent group                                  (<-- tested with this group_id)
|
--- Group                                     (<-- tested with this group_id)
     |
     --- Repository (git project)
     |
     --- Child group                          (<-- tested with this group_id)

And in all of the cases, I faced the same error message. Can someone please explain to me how the group_id in protected branches is supposed to work?