-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprofile.ps1
38 lines (33 loc) · 1.73 KB
/
profile.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
# Azure Functions profile.ps1
#
# This profile.ps1 will get executed every "cold start" of your Function App.
# "cold start" occurs when:
#
# * A Function App starts up for the very first time
# * A Function App starts up after being de-allocated due to inactivity
#
# You can define helper functions, run commands, or specify environment variables
# NOTE: any variables defined that are not environment variables will get reset after the first execution
# You can also define functions or aliases that can be referenced in any of your PowerShell functions.
function Get-SpotifyAccessToken {
[CmdletBinding()]
param ()
# Set Application credentials from Application Settings
$ApplicationCredentials = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("$($env:SPOTIFY_CLIENT_ID)`:$($env:SPOTIFY_CLIENT_SECRET)"))
# Set Request elements
$TokenHeader = @{ 'Authorization' = "Basic $ApplicationCredentials" }
$TokenBody = @{ grant_type = 'refresh_token'; refresh_token = "$($env:SPOTIFY_REFRESH_TOKEN)" }
try {
# Get an Access token from the Refresh token
$AccessToken = Invoke-RestMethod -Method Post -Headers $TokenHeader -Uri 'https://accounts.spotify.com/api/token' -Body $TokenBody | Select-Object -ExpandProperty access_token
} catch {
Write-Error "Error getting Access Token from Spotify API '/token' endpoint using Refresh Token: $_"
}
# Get OAuth2 Access token and set API headers
if ($AccessToken) {
$ApiHeaders = @{ 'Authorization' = "Bearer $AccessToken" }
} else {
Write-Error 'No OAuth2 Access token was granted. Please ensure that the application ClientID and ClientSecret, and the user OAuth2 Refresh token are valid.'
}
return $ApiHeaders
}