Search code examples
coldfusionhttp-postgoogle-workspace

Google g suite Invalid Given/Family Name: FamilyName (ColdFusion/HTTP POST)


I have seen several posts about this issue, but I haven't been able to overcome mine using the suggestions. Following is my code and the results:

<cffunction name="CreateUser" hint="Create new GSuite user." returntype="struct" access="public">
     <cfargument name="token" hint="The Google-provided access token." type="string" required="yes">
     <cfargument name="state" hint="The unique anti-forgery string." type="string" required="yes">
     <cfargument name="userdata" hint="A json string containing user data" type="string" required="yes">
     <cfargument name="api" hint="API Key for the Google Domain" type="string" required="yes">

     <cfhttp method="post" charset="utf-8" url="https://www.googleapis.com/admin/directory/v1/users?state=#state#&access_token=#token#" result="uResult">
          <cfhttpparam type="header" name="auth" value="Authorization: Bearer #token#">
          <cfhttpparam type="header" name="accept" value="Accept: application/json">
          <cfhttpparam type="header" name="content" value="Content-type: application/json">
          <cfhttpparam type="body" name="body" value=#userdata#>
     </cfhttp>

     <cfreturn uResult>
</cffunction>

The JSON string being used is:

{
  "password":"Test@me12!",
  "primaryEmail":"John@doe.com",
  "name":
    {
     "familyName":"Doe",
     "givenName":"John"
    }
}

I am taking my HTTP POST structure from the following example: Google Users: insert (Directory API)

The result I get back from Google is this:

{
  "error": {
    "errors": [
      {
       "domain": "global",
       "reason": "invalid",
       "message": "Invalid Given/Family Name: FamilyName"
      }
   ],
   "code": 400,
   "message": "Invalid Given/Family Name: FamilyName"
  }
}

I cannot understand what might be wrong. Per other posts, I have included the content-type. Per Google's example, I have included the accept header and the auth header. Still, I cannot get a different result.

If I take the JSON string I pass to the function and use it in the Google link above, I am able to create the user. However, if I pass it through HTTP POST, I cannot. Please tell me there is a stray comma or missing dot somewhere in there.


Solution

  • Your header "name" and "values" looks off. Don't put the header names inside the "value" attribute. That's what the "name" attribute is for :-)

    For example the authorization and disposition headers should be:

     <cfhttpparam type="header" name="Content-Type" value="application/json"> 
     <cfhttpparam type="header" name="Authorization" value="Bearer #token#">
    

    Also, the "body" parameter has no "name" attribute, only a "value":

    <cfhttpparam type="body" value="#userdata#"> 
    

    For more details, see also the cfhttpparam documentation.