Forum Discussion

phoneboy's avatar
phoneboy
Director
5 years ago

API request to Approve/Deny request to join a Group Hub Doesn't Seem to Work

I'm writing a component that will automatically approve a request to join a closed Group Hub.
This will be included in the relevant page quilt and is intended to replicate the functionality of legacy Groups where a group was Open but you had to explicitly join it before you saw content.
Looking at the relevant API documentation, it seems like it would be a relatively straightforward thing to implement.

The component looks like the following:

<#import "khoros-user-utils.ftl" as userUtils />

<#if user.registered >
  <#assign uid = user.id?c />
  <#assign node = coreNode.id />
  <#assign messagePostBody = {
      "type": "membership_request",
      "approve_request": true,
      "user_id": uid
  } />
  <#assign url = "/nodes/grouphub:${node}/membership_requests" />
  <#assign resp = restadmin("2.0",url,"PUT",messagePostBody) />

</#if>

 

The API always returns "Membership Request not found" even though there is definitely a membership request for the relevant user in the relevant Group Hub:

{
  "status" : "success",
  "message" : "",
  "http_code" : 200,
  "data" : {
    "type" : "membership_requests",
    "list_item_type" : "membership_request",
    "size" : 1,
    "items" : [ {
      "type" : "membership_request",
      "user" : {
        "type" : "user",
        "id" : "226",
        "href" : "/users/226",
        "view_href" : "/t5/user/viewprofilepage/user-id/226",
        "login" : "Doug"
      },
      "request_to_join_date" : "2020-09-18T00:56:56.885+02:00"
    } ]
  },
  "metadata" : { }
}

 

Is there a bug with the API call or am I calling it incorrectly?

  • SuzieH's avatar
    SuzieH
    Khoros Alumni (Retired)

     

    Hi phoneboy 

     

     

    The only difference I can see between your example and the documentation is that the docs show the body parameters wrapped in data object.  Maybe try this format?

     

    {
      "data" : {
        "type": "membership_request",
          "approve_request": true,
          "user_id": uid
       }
    }

     

    You might also try the restBuilder FreeMarker method.

    Let me know if one of those options don't work and I'll keep digging.

    • phoneboy's avatar
      phoneboy
      Director

      Tried that format, same result.
      The error returned, if it's helpful:

      {
       "status":"error",
       "message":"Membership Request not found.",
       "data": {
         "type":"error_data",
         "code":505,
         "developer_message":"",
         "more_info":""
       },
       "metadata":{}
      }

       

      Making the same call using restBuilder:

        <#assign approve = restBuilder()
          .method("PUT")
          .path("/nodes/grouphub:" + node + "/membership_requests")
          .body({ "data" : {
            "type": "membership_request",
            "approve_request": "true",
            "user_id": uid
          } })
          .admin(true) />
        <#assign resp = approve.call() />

       

      I get the dreaded Freemarker error dump with this being the only potentially meaningful error:

      Caused by: freemarker.template.TemplateModelException: unexpected exception during rest call: FreemarkerRestCall{version='2.0', restCall=RestCall{path=[nodes, grouphub:myCommunity, membership_requests], action=PUT}, adminCall=true, impersonationParameterSource=null}

      On my page quilt, it says "Widget cannot be displayed."

      • phoneboy's avatar
        phoneboy
        Director

        Well, I figured out what the issue was.
        On a lark, I tried a raw API call with curl, which actually worked:

         

        phoneboy-cli:~ $ curl -u mystageuser:mystagepass -X PUT https://mystageurl/api/2.0/nodes/grouphub:myGroupHub/membership_requests -H 'Content-Type: application/json' -H 'li-api-session-key: mysessiontoken' -d '{ "data": { "type": "membership_request", "approve_request": true, "user_id": 226 } } '

         

        That helped me figure out uid was being interpreted as a string versus a number, which is what the API expects.
        Somehow, I thought putting ?c would resolve this, but instead I need to replace  that with ?number.

        Note this still doesn't solve the issue with restBuilder, which crashes even with the above change.
        That said, I at least have a working solution now.

        I really wish the Community API gave more meaningful error messages.
        Most of the ones I've encountered have been...terribly unhelpful.