Skip to content

Commit

Permalink
Merge pull request #1 from mrmueller/main
Browse files Browse the repository at this point in the history
Allow Edge, Improve authentication
  • Loading branch information
pcgeek86 authored Jul 9, 2023
2 parents d7581dc + e3f0006 commit ac0ff58
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
5 changes: 3 additions & 2 deletions functions/Get-YouTubeChannel.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ function Get-YouTubeChannel {
[CmdletBinding()]
param (
[Parameter(ParameterSetName = 'ChannelId')]
[string[]] $Id
[string[]] $Id,
[switch] $Raw
)
$Uri = 'https://www.googleapis.com/youtube/v3/channels?part=brandingSettings,contentDetails,contentOwnerDetails,id,snippet,localizations,statistics,status,topicDetails'

Expand All @@ -24,4 +25,4 @@ function Get-YouTubeChannel {

if ($Raw) { return $Result }
$Result.items
}
}
33 changes: 16 additions & 17 deletions functions/Grant-YouTubeOauth.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,28 @@ function Grant-YoutubeOauth {
Start-PodeServer -ScriptBlock {
Add-PodeEndpoint -Port 8000 -Protocol Http
Add-PodeRoute -Method Get -Path /auth/complete -ScriptBlock {
@{
access_token = $WebEvent.Query['access_token']
} | ConvertTo-Json | Set-Content -Path $HOME/.pwsh.youtube.oauth.json

$Response = @'
<h1 style="font-family: sans-serif;">Authentication Complete</h1>
<h3 style="font-family: sans-serif;">You may close this browser window.</h3>
<script>
console.log(window.location.hash);
let access_token_regex = /access_token=(?<access_token>.*?)&token_type/;
let result = access_token_regex.exec(window.location.hash);
fetch(`/auth/complete?access_token=${result.groups.access_token}`);
fetch(`/auth/receive?access_token=${result.groups.access_token}`);
</script>
'@
Write-PodeHtmlResponse -Value $Response
Write-PodeHtmlResponse -Value $Response -StatusCode 200
}

Add-Poderoute -Method Get -Path /auth/receive -ScriptBlock{
Out-PodeVariable -Name access_token -Value $WebEvent.Query['access_token']
Close-PodeServer
}
}
}
$token = @{
access_token = $access_token
}
$token | ConvertTo-Json | Out-File -Path $HOME/.pwsh.youtube.oauth.json
}

$Client = Get-Content -Path $ConfigPath | ConvertFrom-Json
Expand All @@ -43,16 +48,10 @@ function Grant-YoutubeOauth {
'https://www.googleapis.com/auth/youtube.force-ssl'
'https://www.googleapis.com/auth/youtube.readonly'
)
$Uri = 'https://accounts.google.com/o/oauth2/v2/auth?include_granted_scopes=true&response_type=token&client_id={0}&redirect_uri={1}&scope={3}&state={2}' -f $Client.client_id, $RedirectUri, (New-Guid).Guid, ($ScopeList -join ' ')
$scopesURI = [System.Web.HttpUtility]::UrlEncode($ScopeList -join ' ')
$Uri = 'https://accounts.google.com/o/oauth2/v2/auth?include_granted_scopes=true&response_type=token&client_id={0}&redirect_uri={1}&scope={2}&state={2}' -f $Client.client_id, $RedirectUri, ($scopesURI), (New-Guid).Guid

$Browser = $BrowserCommand ? $BrowserCommand : (Find-Browser)
Write-Verbose -Message ('Browser command line is: ' -f $Browser)
Start-Process -FilePath $Browser -ArgumentList ('"{0}"' -f $Uri) -Wait

if (!$IsMacOS) {
Stop-Job -Name $JobName
}
else {
Write-Warning -Message 'Please manually stop the Background Job running the Pode web server after completing authentication. Cannot wait for browser process to complete on MacOS.'
}
}
Start-Process -FilePath $Browser -ArgumentList ('"{0}"' -f $Uri)
}
1 change: 1 addition & 0 deletions functions/private/Find-Browser.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ function Find-Browser {
'firefox.exe'
'chrome'
'firefox'
'MicrosoftEdge.exe'
)
foreach ($Command in $CommandList) {
if (Get-Command -Name $Command) { return $Command }
Expand Down

0 comments on commit ac0ff58

Please sign in to comment.