Yammer REST API: How to bulk-add members to a yammer group
So you want to add multiple users to a yammer group via the API?
This question comes up quite often partly because there is no yammer documented/supported api on how to add members to a group in bulk. Most users, out of desperation end up hacking endpoints and and their respective parameters by capturing fiddler trace, inspecting browser elements, etc, but these get them into an unsupported state as undocumented APIs could change and/or break at any time without prior notice.
In this blog post, I will share my experience on how I worked with some clients to bulk add users to yammer group programatically - using yammer supported/documented API endpoints.
Prerequisites
- Register an app in Yammer as discussed in "Yammer REST API for Dummies"
- Note the client ID and secret of the created app
- Create a service account in Yammer and grant verified admin privilege to the service account.
- Obtain OAuth token of the service account as discussed in "Consuming Yammer RESTful API with AngularJS for Dummies"
- Obtain User IDs of the users you'd like to add to a group. These User IDs can be obtained by performing user data export
The Pseudo-code
This implementation is represented in pseudo-code so as to make it easier for you or your developers to translate this information into any programming language of choice.
#Declare variables and constants
Array temp = Read userIDs from csv
constant group_id = 123
constant client_id = c456
var userid = 0;
ImpersonationURL = https://api.yammer.com/api/v1/oauth/tokens.json?user_id=$userid&consumer_key=$client_id
GroupURL = https://api.yammer.com/api/v1/group_memberships.json?group_id=$group_id
while temp contains value {
userid = temp
#Impersonate the user with the VA's OAuth token
json_response = HTTP.GET (ImpersonationURL, userid).Authorization.Header(Bearer verified_admin_token)
#Consume the impersonated user's token from the JSON response
if json_response == null
exit and/or catch NullPointerException
else
#Parse the response to type json and read the token value
user_token = JSON.Parse(json_response).token
#Now, add the impersonated user to the group
response = HTTP.POST (GroupURL, group_id).Authorization.Header(Bearer user_token)
if (response.status == 201) {
render SUCCESS or write userid into success.log
} else {
Read JSON response and render appropriate message OR write userid with the corresponding http response into failed.log
}
#Do not exceed yammer api rate limit - https: //developer.yammer.com/docs/rest-api-rate-limits. So sleep
sleep(2 sec)
increment++
}
In plain English, what we have done so far are:
- Read userIDs from the csv that was obtained from data export
- Iterate through the user list
- Impersonate each user so as to get a pre-authorized token
- Make the user add him/herself to the group. Yah!
I think this is needless to mention, but due to some questions that came up recently, I would like to note that it is not possible to impersonate a pending user. Impersonation only applies to active yammer accounts.
References
- https://developer.yammer.com/v1.0/docs/impersonation
- https://developer.yammer.com/v1.0/docs/group_membershipsjsongroup_idid
- https://developer.yammer.com/docs/rest-api-rate-limits
Was this information helpful? Please leave a feedback.
[bing_translator]