Last active
August 19, 2019 14:39
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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