Public/New-GraphOauthAccessToken.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<#
    .NOTES
    ===========================================================================
     Created with: SAPIEN Technologies, Inc., PowerShell Studio 2017 v5.4.135
     Created on: 2/9/2017 5:46 AM
     Created by: Mark Kraus
     Organization: Mitel
     Filename: New-GraphOauthAccessToken.ps1
    ===========================================================================
    .DESCRIPTION
        New-GraphOauthAccessToken Function
#>


<#
    .SYNOPSIS
        Creates an MSGraphAPI.Oauth.AccessToken Object
    
    .DESCRIPTION
        This creates a MSGraphAPI.Oauth.AccessToken object. This only creates the objects used in this module. It does not make any API calls. To retrieve an OAuth Access Token, use Get-GraphOauthAccessToken
    
    .PARAMETER Application
        A MSGraphAPI.Application object. See New-GraphApplication
    
    .PARAMETER AccessTokenCredential
        A PSCredential Object containing the access_token as a password. Username is ignored.
    
    .PARAMETER RefreshTokenCredential
        A PSCredential Object containing the refresh_token as a password. Username is ignored.
    
    .PARAMETER RequestedDate
        The date and time the current access_token was requested
    
    .PARAMETER Response
        A PSObject containing the last response from the API converted from JSON and striped of the access_token and refresh_token
    
    .PARAMETER ResponseHeaders
        A headers dictionary retruned from the API.
    
    .PARAMETER LastRequestDate
        A datetime of the last API call made using thie token.
    
    .PARAMETER Session
        The Session object used to access the API. This creates a consistent experience accross API cals by mimicing a browser session.
    
    .PARAMETER GUID
        A GUID to identify the Graph OAuth Token Object. If one is not provided, a new GUID will be generated. This is used for internal reference only and is not consumed by the Graph API.
    
    .EXAMPLE
                PS C:\> New-GraphOauthAccessToken -Application $GraphApp -AccessTokenCredential $AccessTokenCredential -RefreshTokenCredential $RefreshTokenCredential -RequestedDate (get-date) -Response $Response -ResponseHeaders $Result.Headers -LastRequestDate (get-date)
    
    .NOTES
        See Get-GraphOauthAccessToken
    
    .OUTPUTS
        MSGraphAPI.Oauth.AccessToken
#>

function New-GraphOauthAccessToken {
    [CmdletBinding(ConfirmImpact = 'Low')]
    [OutputType('MSGraphAPI.Oauth.AccessToken')]
    param
    (
        [Parameter(Mandatory = $true,
                   ValueFromPipelineByPropertyName = $true)]
        [ValidateNotNullOrEmpty()]
        [PSTypeName('MSGraphAPI.Application')]$Application,
        
        [Parameter(Mandatory = $true,
                   ValueFromPipelineByPropertyName = $true)]
        [ValidateNotNullOrEmpty()]
        [System.Management.Automation.PSCredential]$AccessTokenCredential,
        
        [Parameter(Mandatory = $true,
                   ValueFromPipelineByPropertyName = $true)]
        [ValidateNotNullOrEmpty()]
        [System.Management.Automation.PSCredential]$RefreshTokenCredential,
        
        [Parameter(Mandatory = $true,
                   ValueFromPipelineByPropertyName = $true)]
        [datetime]$RequestedDate,
        
        [Parameter(Mandatory = $true,
                   ValueFromPipelineByPropertyName = $true)]
        [System.Management.Automation.PSObject]$Response,
        
        [Parameter(Mandatory = $true,
                   ValueFromPipelineByPropertyName = $true)]
        [System.Management.Automation.PSObject]$ResponseHeaders,
        
        [Parameter(Mandatory = $true,
                   ValueFromPipelineByPropertyName = $true)]
        [ValidateNotNullOrEmpty()]
        [datetime]$LastRequestDate,
        
        [Parameter(Mandatory = $false,
                   ValueFromPipeline = $false)]
        [Microsoft.PowerShell.Commands.WebRequestSession]$Session,
        
        [Parameter(Mandatory = $false,
                   ValueFromPipelineByPropertyName = $true)]
        [guid]$GUID = [Guid]::NewGuid()
    )
    
    Process {
        [pscustomobject]@{
            PSTypeName = 'MSGraphAPI.Oauth.AccessToken'
            Application = $Application
            AccessTokenCredential = $AccessTokenCredential
            RefreshTokenCredential = $RefreshTokenCredential
            RequestedDate = $RequestedDate
            Response = $Response | Select-Object -property * -ExcludeProperty access_token, refresh_token
            ResponseHeaders = $ResponseHeaders
            LastRequestDate = $LastRequestDate
            Session = $Session
            GUID = $GUID
        }
    }
}