question

43899532 avatar image
0 Votes"
43899532 asked 43899532 commented

Exchange Online with OAuth2.0 I got Error

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()

windows-server-powershelloffice-exchange-online-itprooffice-exchange-server-dev
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @TaikiYamashita-8831

Since your question is related to Exchange Development, I have added the tag “office-exchange-server-dev” to it.
Thanks for your understanding.

1 Vote 1 ·

1 Answer

GlenScales-6756 avatar image
0 Votes"
GlenScales-6756 answered 43899532 commented

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



· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

This problem was resolved thanks to your support.

0 Votes 0 ·