I am following this link to connect to Microsoft Teams using Access Tokens:
https://learn.microsoft.com/en-us/MicrosoftTeams/teams-powershell-application-authentication
Here is the code:
$ClientSecret = 'xxxxx~xx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
$ApplicationID = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
$TenantID = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
$graphtokenBody = @{
Grant_Type = "client_credentials"
Scope = "https://graph.microsoft.com/.default"
Client_Id = $ApplicationID
Client_Secret = $ClientSecret
}
$graphToken = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$TenantID/oauth2/v2.0/token" -Method POST -Body $graphtokenBody | Select-Object -ExpandProperty Access_Token
$teamstokenBody = @{
Grant_Type = "client_credentials"
Scope = "48ac35b8-9aa8-4d74-927d-1f4a14a0b239/.default"
Client_Id = $ApplicationID
Client_Secret = $ClientSecret
}
$teamsToken = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$TenantID/oauth2/v2.0/token" -Method POST -Body $teamstokenBody | Select-Object -ExpandProperty Access_Token
Connect-MicrosoftTeams -AccessTokens @("$graphToken", "$teamsToken")
The application is registered in Azure AD and has been assigned the API permissions as mentioned in the article. I have assigned Teams administrator role to the application as well. Tokens get populated correctly.
Running the code produces the following error:
Connect-MicrosoftTeams : Object reference not set to an instance of an object.
At line:29 char:1
Connect-MicrosoftTeams -AccessTokens @("$graphToken", "$teamsToken")
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : AuthenticationError: (:) [Connect-MicrosoftTeams], NullReferenceException
FullyQualifiedErrorId : Connect-MicrosoftTeams,Microsoft.TeamsCmdlets.Powershell.Connect.ConnectMicrosoftTeams
Connect-MicrosoftTeams : Object reference not set to an instance of an object.
At line:29 char:1
Connect-MicrosoftTeams -AccessTokens @("$graphToken", "$teamsToken")
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : NotSpecified: (:) [Connect-MicrosoftTeams], NullReferenceException
FullyQualifiedErrorId : System.NullReferenceException,Microsoft.TeamsCmdlets.Powershell.Connect.ConnectMicrosoftTeams
If someone had this issue and have successfully resolved it or have some tips on how to resolve it, please help.
CodePudding user response:
I tried to reproduce the same in my environment and got the same error as below:
The error usually occurs if the version of Microsoft teams you are trying to connect is older than 4.7.1-preview
version.
To resolve the error, try updating the Microsoft teams PowerShell version to 4.7.1-preview
or later and this type of authentication is supported only in commercial environments.
Get-InstalledModule -Name MicrosoftTeams
Update-Module -Name MicrosoftTeams
After updating the PowerShell Module, I am able to connect to Microsoft Teams successfully like below:
$ClientSecret = "ClientSecret"
$ApplicationID = "AppID"
$TenantID = "TenantID"
$graphtokenBody = @{
Grant_Type = "client_credentials"
Scope = "https://graph.microsoft.com/.default"
Client_Id = $ApplicationID
Client_Secret = $ClientSecret
}
$graphToken = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$TenantID/oauth2/v2.0/token" -Method POST -Body $graphtokenBody | Select-Object -ExpandProperty Access_Token
$teamstokenBody = @{
Grant_Type = "client_credentials"
Scope = "48ac35b8-9aa8-4d74-927d-1f4a14a0b239/.default"
Client_Id = $ApplicationID
Client_Secret = $ClientSecret
}
$teamsToken = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$TenantID/oauth2/v2.0/token" -Method POST -Body $teamstokenBody | Select-Object -ExpandProperty Access_Token
Connect-MicrosoftTeams -AccessTokens @("$graphToken", "$teamsToken")
Reference: