internal static string AcquireToken(string resource, string scope = null) { GenericToken token = null; if (PnPConnection.CurrentConnection == null) { return(null); } var tenantId = TenantExtensions.GetTenantIdByUrl(PnPConnection.CurrentConnection.Url); if (PnPConnection.CurrentConnection.PSCredential != null) { if (scope == null) { // SharePoint or Graph V1 resource var scopes = new[] { $"https://{resource}//.default" }; token = GenericToken.AcquireDelegatedTokenWithCredentials(PnPConnection.PnPManagementShellClientId, scopes, "https://login.microsoftonline.com/organizations/", PnPConnection.CurrentConnection.PSCredential.UserName, PnPConnection.CurrentConnection.PSCredential.Password); } else { token = GenericToken.AcquireDelegatedTokenWithCredentials(PnPConnection.PnPManagementShellClientId, new[] { scope }, "https://login.microsoftonline.com/organizations/", PnPConnection.CurrentConnection.PSCredential.UserName, PnPConnection.CurrentConnection.PSCredential.Password); } } else if (!string.IsNullOrEmpty(PnPConnection.CurrentConnection.ClientId) && !string.IsNullOrEmpty(PnPConnection.CurrentConnection.ClientSecret)) { var clientId = PnPConnection.CurrentConnection.ClientId; var clientSecret = HttpUtility.UrlEncode(PnPConnection.CurrentConnection.ClientSecret); if (scope == null && !resource.Equals("graph.microsoft.com", System.StringComparison.OrdinalIgnoreCase)) { // SharePoint token var scopes = new[] { $"https://{resource}//.default" }; token = GenericToken.AcquireApplicationToken(tenantId, clientId, "https://login.microsoftonline/organizations/", scopes, clientSecret); } else { token = GenericToken.AcquireApplicationToken(tenantId, clientId, "https://login.microsoftonline.com/organizations/", new[] { scope }, clientSecret); } } if (token != null) { return(token.AccessToken); } return(null); }