Exchange Online with OAuth2.0 I got Error

Taiki Yamashita 21 Reputation points
2022-05-16T06:00:55.517+00:00

Im trying to make Receiving emails from Exchange Online with OAuth2.0.
Now Im trying to get AuthToken but I Got error below.
May I help me about this Error.

Cannot find an overload for "AcquireTokenByUsernamePassword" and the argument count: "3".
At line:17 char:5

  • $PublicClientApplication.AcquireTokenByUsernamePassword($scopes, ...
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : NotSpecified: (:) [], MethodException
  • FullyQualifiedErrorId : MethodCountCouldNotFindBest

My Script:
$ClientId = "____________________________________"
$TenantId = "____________________________________"
$UserName = '____________@_______.onmicrosoft.com'
$Pass = '_______'

[string]$Scopes = @("https://outlook.office365.com/IMAP.AccessAsUser.All","https://outlook.office365.com/SMTP.Send")  
$securePass = ConvertTo-SecureString $Pass -AsPlainText -Force  
$Options = [Microsoft.Identity.Client.PublicClientApplicationOptions]::new()  
$Options.ClientId = $ClientID  
$Options.TenantId = $TenantID  
# $Options.RedirectUri = $RedirectUri  

$PublicClientApplication = [Microsoft.Identity.Client.PublicClientApplicationBuilder]::CreateWithApplicationOptions($Options).Build()  

$AuthToken = $PublicClientApplication.AcquireTokenByUsernamePassword($scopes, $UserName, $securePass).ExecuteAsync()  
Microsoft Exchange Online Management
Microsoft Exchange Online Management
Microsoft Exchange Online: A Microsoft email and calendaring hosted service.Management: The act or process of organizing, handling, directing or controlling something.
4,175 questions
Exchange Server Development
Exchange Server Development
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Development: The process of researching, productizing, and refining new or existing technologies.
507 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,363 questions
{count} votes

Accepted answer
  1. Glen Scales 4,431 Reputation points
    2022-05-18T02:02:41.137+00:00

    The AcquireTokenByUsernamePassword method is expecting an IEnumerable for the scopes so i believe that's what is complaining about I would suggest you try

     $Scopes = New-Object System.Collections.Generic.List[string]
     $Scopes.Add("https://outlook.office365.com/IMAP.AccessAsUser.All")
     $Scopes.Add("https://outlook.office365.com/SMTP.Send")
     $securePass = ConvertTo-SecureString $Pass -AsPlainText -Force
     $Options = [Microsoft.Identity.Client.PublicClientApplicationOptions]::new()
     $Options.ClientId = $ClientID
     $Options.TenantId = $TenantID
     # $Options.RedirectUri = $RedirectUri
    
     $PublicClientApplication = [Microsoft.Identity.Client.PublicClientApplicationBuilder]::CreateWithApplicationOptions($Options).Build()
     $AuthToken = $PublicClientApplication.AcquireTokenByUsernamePassword($Scopes, $UserName, $securePass).ExecuteAsync().GetAwaiter().GetResult()
    

    which works okay for me


0 additional answers

Sort by: Most helpful