Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # This file shouldn't be run on it's own. It should be loaded using the Apigee Module.
- <#
- .SYNOPSIS
- Makes a call to the Apigee OAuth login endpoint and gets access tokens to use.
- This should be used internally by the Apigee module. But, it shouldn't be needed by
- the developer.
- .EXAMPLE
- $result = Get-ApigeeAccessTokens
- #>
- Function Get-ApigeeAccessTokens {
- [CmdletBinding()]
- [OutputType([PSCustomObject])]
- Param ()
- $otp = Get-ApigeeOTP `
- -SharedSecret $global:Apigee.OAuthLogin.OTPSharedSecret `
- -Email $global:Apigee.OAuthLogin.Username
- $body = @{
- username = $global:Apigee.OAuthLogin.Username
- password = $global:Apigee.OAuthLogin.Password
- mfa_token = $otp.OTP
- grant_type = "password"
- }
- $results = Invoke-WebRequest `
- -Uri $global:Apigee.OAuthLogin.Url `
- -Method $global:Apigee.OAuthLogin.Method `
- -Headers $global:Apigee.OAuthLogin.Headers `
- -ContentType $global:Apigee.OAuthLogin.ContentType `
- -Body $body
- if($results.StatusCode -ne 200) {
- $resultsAsString = $results | Out-String
- throw "Authentication with Apigee's OAuth Failed. `r`n`r`nFull Response Object:`r`n$resultsAsString"
- }
- $resultsObj = ConvertFrom-Json -InputObject $results.Content
- $resultsObj = Add-PsType -PSObject $resultsObj -PsType $global:Apigee.OAuthLogin.ResultObjectName
- Set-ApigeeAuthHeader -Authorization $resultsObj.access_token
- return $resultsObj
- }
- <#
- .SYNOPSIS
- Sets $global:Apigee.AuthHeader @{ Authorization = "value passed in" }
- This is used to authenticate all calls to the Apigee REST Management endpoints.
- .EXAMPLE
- Set-ApigeeAuthHeader -Authorization "Bearer ..."
- #>
- Function Set-ApigeeAuthHeader {
- [CmdletBinding()]
- [OutputType([PSCustomObject])]
- Param (
- [Parameter(Mandatory = $true)]
- $Authorization
- )
- $bearerAuth = "Bearer $Authorization"
- $global:Apigee.AuthHeader = @{ Authorization = $bearerAuth }
- }
- [string[]]$funcs =
- "Get-ApigeeAccessTokens", "Set-ApigeeAuthHeader"
- Export-ModuleMember -Function $funcs
Add Comment
Please, Sign In to add comment