Skip to content

Instantly share code, notes, and snippets.

@gautamdsheth
Last active November 23, 2017 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gautamdsheth/32d864b9168e1405e81b792977f01d4a to your computer and use it in GitHub Desktop.
Save gautamdsheth/32d864b9168e1405e81b792977f01d4a to your computer and use it in GitHub Desktop.
class Program
{
static void Main(string[] args)
{
Task.Run(() => MainAsync());
Console.ReadLine();
}
}
static async Task MainAsync()
{
string userName = "user@tenantName.onmicrosoft.com";
string password = "password";
List<KeyValuePair<string, string>> vals = new List<KeyValuePair<string, string>>();
string tenantName = "tenantName.OnMicrosoft.com";
string authString = "https://login.microsoftonline.com/" + tenantName;
string resource = "https://graph.microsoft.com";
AuthenticationContext authenticationContext = new AuthenticationContext(authString, false);
string clientId = "<client-id>";
string key = "<client-secret>";
vals.Add(new KeyValuePair<string, string>("client_id", clientId));
vals.Add(new KeyValuePair<string, string>("resource", resource));
vals.Add(new KeyValuePair<string, string>("username", userName));
vals.Add(new KeyValuePair<string, string>("password", password));
vals.Add(new KeyValuePair<string, string>("grant_type", "password"));
vals.Add(new KeyValuePair<string, string>("client_secret", key));
string url = string.Format("https://login.windows.net/{0}/oauth2/token", tenantName);
using (HttpClient httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Add("Cache-Control", "no-cache");
HttpContent content = new FormUrlEncodedContent(vals);
HttpResponseMessage hrm = httpClient.PostAsync(url, content).Result;
AuthenticationResponse authenticationResponse = null;
if (hrm.IsSuccessStatusCode)
{
Stream data = await hrm.Content.ReadAsStreamAsync();
DataContractJsonSerializer serializer = new
DataContractJsonSerializer(typeof(AuthenticationResponse));
authenticationResponse = (AuthenticationResponse)serializer.ReadObject(data);
var accessToken = authenticationResponse.access_token;
Stream groupLogoStream = new FileStream("C:\\groupassets\\logo-original.png",
FileMode.Open, FileAccess.Read);
var group = UnifiedGroupsUtility.CreateUnifiedGroup("displayName", "description",
"MyModernTeamSite", accessToken, groupLogo: groupLogoStream);
string groupUrl = group.SiteUrl;
string groupId = group.GroupId;
// Get all groups in the tenant
//List<UnifiedGroupEntity> groups = UnifiedGroupsUtility.ListUnifiedGroups(accessToken);
// Update description and group logo programatically
//groupLogoStream = new FileStream("C:\\groupassets\\logo-new.png", FileMode.Open, FileAccess.Read);
//UnifiedGroupsUtility.UpdateUnifiedGroup(groupId, accessToken, description: "Updated description", groupLogo: groupLogoStream);
// Delete group programatically
//UnifiedGroupsUtility.DeleteUnifiedGroup(groupId, accessToken);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment