Search code examples
gogoogle-apis-explorergoogle-directory-api

Google Directory API add Custom Schema/Update it to Users per google API (in go)


I am trying to upload a CustomSchema to all Users of a company in GSuite. This Custom Schema contains their Github Usernames, which I extracted with the github API.

The problem is, after running the code, the account in Gsuite is not added.

Relevant code (A connection to GSuite with admin Authentication is established, the map has all user entries. If you still want more code, I can provide you with it - just trying to keep it simple):

for _, u := range allUsers.Users {

    if u.CustomSchemas != nil {
        log.Printf("%v", string(u.CustomSchemas["User_Names"]))
    }else{
        u.CustomSchemas = map[string]googleapi.RawMessage{}
    }
    nameFromGsuite := u.Name.FullName
    if githubLogin, ok := gitHubAccs[nameFromGsuite]; ok {          

            userSchemaForGithub := GithubAcc{GitHub: githubLogin}
            jsonRaw, err := json.Marshal(userSchemaForGithub)
            if err != nil {
                log.Fatalf("Something went wrong logging: %v", err)
            }

            u.CustomSchemas["User_Names"] = jsonRaw
            adminService.Users.Update(u.Id, u)

    } else {
        log.Printf("User not found for %v\n", nameFromGsuite)
    }
}

This is the struct for the json encoding:

 type GithubAcc struct {
    GitHub string `json:"GitHub"`
}

Solution

  • For anyone stumbling upon this.
    Everything in the code snippet is correct. By the way the method is written, I expected that adminService.Users.Update() actually updates the user. Instead, it returns an UserUpdatesCall.
    You need to execute that update by calling .Do()
    From the API:

    Do executes the "directory.users.update" call.

    So the solution is to change adminService.Users.Update(u.Id, u)
    into adminService.Users.Update(u.Id, u).Do()