-
Notifications
You must be signed in to change notification settings - Fork 0
/
sfapi.ps1
80 lines (63 loc) · 1.8 KB
/
sfapi.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
function sfapi {
[CmdletBinding()]
param(
[Parameter(Mandatory, ValueFromPipeline)]
[string]
$Uri,
[Parameter()]
[ValidateSet('GET', 'POST', 'PUT')]
[String]
$HttpRequestType = "GET",
[Parameter()]
[ValidateSet('json', 'powershell')]
[String]
$OutputMethod = 'powershell',
[Parameter()]
[String]
$CredentialFilePath = "$PSScriptRoot/config/default.json"
)
process {
#
# AUTHENTICATE TO API
#
# Load credentials from JSON file (default stored in ./config/default.json)
$api_creds = (Get-Content -Raw -Path $CredentialFilePath | ConvertFrom-Json)
$body = @{
grant_type = "password"
client_id = $api_creds.api_token_id
client_secret = $api_creds.api_token_secret
username = $api_creds.user_login
password = $api_creds.user_password
}
$params = @{
Method = "POST"
Uri = "https://$($api_creds.subdomain).sharefile.com/oauth/token"
Body = $body
ContentType = 'application/x-www-form-urlencoded'
}
$auth_response = (Invoke-RestMethod @params)
$api_endpoint = "https://$($auth_response.subdomain).$($auth_response.apicp)/sf/v3"
$auth_token_type = $auth_response.token_type
$auth_token = $auth_response.access_token
$auth_http_header_value = "$((Get-Culture).TextInfo.ToTitleCase($auth_token_type)) $auth_token"
#
# EXECUTE API CALL PROVIDED BY USER
#
$headers = @{
"Authorization" = $auth_http_header_value
}
$params = @{
Method = $HttpRequestType.ToUpper()
Uri = "$api_endpoint/$(($Uri).TrimStart('/'))"
ContentType = 'application/x-www-form-urlencoded'
Headers = $headers
}
$query_response = (invoke-restmethod @params )
if ($OutputMethod -eq 'json') {
return $( $query_response | convertto-json)
}
else {
return $query_response
}
}
}