-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathGrant-YouTube.ps1
35 lines (33 loc) · 1.43 KB
/
Grant-YouTube.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
function Grant-Youtube {
<#
.SYNOPSIS
Implementation of device (ie. Android TV) authentication for YouTube APIs.
#>
[CmdletBinding()]
param (
)
$Client = Get-Content -Path $ConfigPath | ConvertFrom-Json
Write-Verbose -Message $Client
$GrantType = 'urn:ietf:params:oauth:grant-type:device_code'
$Uri = 'https://oauth2.googleapis.com/device/code?client_id={0}&scope={1}' -f $Client.client_id, $Scopes
try {
$Response = Invoke-RestMethod -Method Post -Uri $Uri -ErrorAction Stop
}
catch {
throw 'Invalid YouTube app configuration. Check your YouTube application configuration in Google Cloud Platform.'
return
}
Write-Host -Object ('Open your browser and go to https://www.google.com/device, enter code {0}' -f $Response.user_code)
while ((Get-Date) -lt (Get-Date).AddSeconds($Response.expires_in)) {
$ValidateUri = 'https://oauth2.googleapis.com/token?client_id={0}&grant_type={1}&client_secret={2}&device_code={3}' -f $Client.client_id, $GrantType, $Client.client_secret, $Response.device_code
Write-Host -Object $ValidateUri
try {
Invoke-RestMethod -Method Post -Uri $ValidateUri -ErrorVariable validateerror | ConvertTo-Json | Set-Content -Path $HOME/.pwsh.youtube.json
}
catch {
if ($ValidateError) { Write-Host -Object 'Waiting for authorization ...' }
}
if (!$ValidateError) { Write-Host 'Authorization success' -ForegroundColor green; break }
Start-Sleep -Seconds 10
}
}