umn-microsoft-automation / UMN-Google

GNU General Public License v3.0
21 stars 11 forks source link

Auth works in powershell core 7.2.1 but not in powershell 5.1 #54

Closed pierrediancourt closed 7 months ago

pierrediancourt commented 2 years ago

Hello,

When running this basic code made of the Example.ps1 file provided in this repo

#Requires -RunAsAdministrator

Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
Install-Module UMN-Google
Import-Module UMN-Google

# ref : https://jamesachambers.com/modify-google-sheets-using-powershell/

function Get-WebReqErrorDetails {
    <#
        .SYNOPSIS
            Returns JSON Responsbody data from an Error thrown by Invoke-Webrequest or Invoke-RestMethod
        .DESCRIPTION
            Returns JSON Responsbody data from an Error thrown by Invoke-Webrequest or Invoke-RestMethod
        .PARAMETER err
            Error thrown by Invoke-Webrequest or Invoke-RestMethod
        .NOTES
            Author: Travis Sobeck
    #>
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory)]
        [System.Management.Automation.ErrorRecord]$err
    )

    $reader = New-Object System.IO.StreamReader($err.Exception.Response.GetResponseStream())
    $reader.BaseStream.Position = 0
    $reader.DiscardBufferedData()
    return ($reader.ReadToEnd() | ConvertFrom-Json)
}

# Set security protocol to TLS 1.2 to avoid TLS errors
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

# Google API Authozation
# Service token
$scope = "https://www.googleapis.com/auth/spreadsheets https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/drive.file"
$certPath = Resolve-Path "$PSScriptRoot/xxx"
$iss = "xxx" #this is the service account email it will look like  <something>@<projectname>.iam.gserviceaccount.com
$certPswd = "xxx" #Password for certificate
try {
    $accessToken = Get-GOAuthTokenService -scope $scope -certPath $certPath -certPswd $certPswd -iss $iss
} catch {
    (Get-WebReqErrorDetails $_).error_description

    $err = $_.Exception
    $err | Select-Object -Property *
    "Response: "
    $err.Response
}
echo "$accessToken"

I get this error
image

But only when i run it in powershell 5.1
image There's no error thrown and the token is displayed when running in PowerShell 7.2.1
image

My clock is synced with microsoft time servers as it is the default on any W10 machine. I can live with running that with the pwsh binary but i thought it should be referenced somewhere :)

Thanks for your work on that project

travis-sobeck commented 2 years ago

@pierrediancourt huh. I have the exact same PS version and run 'Get-GOAuthTokenService' on a regular basis. Our production automation servers when we created that were 5.x

pierrediancourt commented 2 years ago

Alright, fair enough :D That's probably an issue caused by something else then. I'll be glad to update the title of this issue with truer label if we get to the bottom of this mystery !

ronnybuchmann commented 2 years ago

the actual issue is the system locale / culture settings see https://stackoverflow.com/a/66022792

Breiver7 commented 1 year ago

Alright, fair enough :D That's probably an issue caused by something else then. I'll be glad to update the title of this issue with truer label if we get to the bottom of this mystery !

Hi! I encountered this issue couple days ago when i've changed date format from MM/dd/yyyy to dd.MM.yyyy in my OS settings. After that my code similar to above mentioned ceased to work in powershell 5.1 but was still okay when running in PowerShell 7.2.1. Ultimately i reverted changes with date format back and code was working in powershell 5.1 again Don't know if it's related to this case but i hope this is gonna help "to get to the bottom of this mystery" in a way :D

kylecweeks commented 7 months ago

Item closed as of module 2.0.2 -> pulled in Ronny's update as the fix. All code tested in Ubuntu LTS 22 and Windows 11 (pwsh 7.4 / powerShell 5.1)