title: Authenticate to Microsoft Graph api using powershell
body: <p><strong>*Update</strong> - Hacking away this afternoon and I discovered that I was trying to authenticate using <a href="https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-authentication-libraries" rel="nofollow noreferrer">ADAL authentication</a> when I should be using <a href="https://github.com/AzureAD/microsoft-authentication-library-for-dotnet" rel="nofollow noreferrer">MSAL</a>. I changed my approach to use MSAL, and have been successful, but this question still stands for ADAL auth. </p>
  
  <p>I have been having a difficult time being able to authenticate with the graph API using powershell. My end goal is to be able to query some of my OneNote pages, and I am able to use the <a href="https://developer.microsoft.com/en-us/graph/graph-explorer" rel="nofollow noreferrer">graph explorer</a> to authenticate using my personal Microsoft account.</p>
  
  <p>I have been following <a href="https://blogs.technet.microsoft.com/paulomarques/2016/03/21/working-with-azure-active-directory-graph-api-from-powershell/" rel="nofollow noreferrer">this blog</a>, and this <a href="https://blogs.technet.microsoft.com/cloudlojik/2017/09/05/using-powershell-to-connect-to-microsoft-graph-api/" rel="nofollow noreferrer">more recent</a> one. </p>
  
  <p>I registered my app at <a href="https://apps.dev.microsoft.com" rel="nofollow noreferrer">https://apps.dev.microsoft.com</a> and I have a application ID which I have plugged into the PS script as the <code>$clientID</code> and when I run the script I get an error: <code>Method invocation failed because [Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext] does not contain a method named 'AcquireToken'.</code></p>
  
  <p>Looking at the .net documentation for the <a href="https://docs.microsoft.com/en-us/dotnet/api/microsoft.identitymodel.clients.activedirectory.authenticationcontext?view=azure-dotnet" rel="nofollow noreferrer">Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext</a> class there is no method named <code>AcquireToken</code>, but there are a few similarly named ones, although I cannot figure out which one I need to use. Ideally I would like my script to prompt the user for their credentials similar to how the <code>AzureRM</code> module's <code>Login-AzureRmAccount</code> function does. If that is not possible than I could use guidance on how to rework the code below to use Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext.AcquireTokenASync</p>
  
  <pre><code>function Get-AuthToken
  {
  
       param
           (
           [Parameter(Mandatory=$true)]
           $TenantName
           )
       Import-Module Azure
       $clientId = "00d16af4-d0c7-460a-a9dc-fd350eb4b100" 
       $redirectUri = "urn:ietf:wg:oauth:2.0:oob"
       $resourceAppIdURI = "https://graph.microsoft.com"
       $authority = "https://login.microsoftonline.com/$TenantName"
       $authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList $authority
  $Credential = Get-Credential
       $AADCredential = New-Object   "Microsoft.IdentityModel.Clients.ActiveDirectory.UserCredential" -ArgumentList   $credential.UserName,$credential.Password
       $authResult = $authContext.AcquireToken($resourceAppIdURI, $clientId,$AADCredential)
       return $authResult
  
   }
   Get-AuthToken -TenantName "common"
  </code></pre>