Trying to add aliases to users using post request to Directory (Admin SDK). I know that the JS client libraries is still in beta, but it's the simplest solution for what I have to do.
The below returns a 401 error. My credentials are just fine on Console.
<script>
var apiKey = "my_key";
var clientId = "my_client_id";
var scopes = "https://www.googleapis.com/auth/admin.directory.user.alias";
var users = {}; // external JSON file
function load() {
gapi.client.setApiKey(apiKey);
gapi.auth.authorize({
client_id: clientId,
scope: scopes,
immediate: true
},
function(){
for (var i = 0; i < users.length; i++) {
var thisUser = users[i]["emailAddress"];
var thisAlias = users[i]["secondaryEmail"];
var thisURL = "https://www.googleapis.com/admin/directory/v1/users/" + thisUser + "/aliases"
$.post(thisURL, {alias: thisAlias});
}
});
}
</script>
<script src="https://apis.google.com/js/client.js?onload=load"></script>