Skip to content

Instantly share code, notes, and snippets.

@Natfan
Last active August 19, 2019 14:39
Show Gist options
  • Save Natfan/8f49abf490d5b5164e4b99dfaff90e33 to your computer and use it in GitHub Desktop.
Save Natfan/8f49abf490d5b5164e4b99dfaff90e33 to your computer and use it in GitHub Desktop.
Microsoft Teams Client API Example - Getting OAuth Bearer Token (https://blog.natfan.io/teams/1)
function Get-OAuthToken {
[CmdletBinding()]
[OutputType([string])]
param (
[Parameter(Mandatory)]
[Parameter(Mandatory)][ValidateScript({[GUID]::TryParse($_, [ref][GUID]::Empty)})][String]$TenantID,
[Parameter(Mandatory)][PSCredential][System.Management.Automation.CredentialAttribute()]$Credential
)
try {
$Username = $Credential.Username
$Password = $Credential.Password
# Set Resource URI to Microsoft Teams OAuth API
$resourceAppIDURI = 'https://api.spaces.skype.com'
# Set Authority to Azure AD Tenant
$Authority = 'https://login.microsoftonline.com/common/' + $TenantID
$OAuthCredential = [Microsoft.IdentityModel.Clients.ActiveDirectory.UserPasswordCredential]::new($Username, $Password)
$AuthContext = [Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext]::new($Authority)
$Token = [Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContextIntegratedAuthExtensions]::AcquireTokenAsync($AuthContext, $ResourceAppIDURI, $PowerShellClientID, $OAuthCredential)
$Token.Wait() # Wait for the async function to finish.
} catch {
throw $_
$ErrorMessage = 'Failed to aquire OAuth token.'
Write-Error -Message 'Failed to aquire OAuth token'
break
}
return $Token
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment