Welcome to the Panopto Community

Please note: All new registrants to the Panopto Community Forum must be approved by a forum moderator or admin. As such, if you navigate to a feature that is members-only, you may receive an error page if your registration has not yet been approved. We apologize for any inconvenience and are approving new members as quickly as possible.
Options

API Authentication Token in PowerShell

I'm trying to get an API auth token with a User-based server Application in PowerShell, and I'm getting an "invalid_client" error.


The client ID is definitely correct, and I've tried a few different ways of making the call.



[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::TLS12

$CLIENTID = "****"

$CLIENTSECRET = ""****"

$Text = "$($CLIENTID):$($CLIENTSECRET)"

$Bytes = [System.Text.Encoding]::Unicode.GetBytes($Text)

$EncodedText =[Convert]::ToBase64String($Bytes)


$AuthHeader = @{"Content-Type" = "application/x-www-form-urlencoded"

"Authorization" = "Basic $($EncodedText)"

}


$AuthBody  = @{"Grant_type"  = "password"

          "Scope" = "API"

"Username" = ""****"

"Password" = ""*****"

}

$uri = "https://penncareylaw.hosted.panopto.com/Panopto/oauth2/connect/token"


$request = Invoke-RestMethod -Method POST -Uri $uri -Body $AuthBody -Headers $AuthHeader



I'm stumped - does anyone know how to make this call in PowerShell?


Thanks!

Best Answer

  • Options
    Nedim DeliahmetovicNedim Deliahmetovic Panopto Employee
    Answer ✓

    Hi Amelia,

    I am sorry that you still have issues.

    The error "invalid_client" is mostly related to the invalid data about client (client id, client secret or grant type).

    Can you please try to get Access Token using Postman with your client id, client secret and username and password? I am sharing with you required Posman values for request:

    If you are unable to get AccessToken using Postman, try to regenerate Client Secret for Client that you are using and please verify that generated client secret is copied without extra spaces.


    Regards.

    Nedim

Answers

  • Options
    Nedim DeliahmetovicNedim Deliahmetovic Panopto Employee

    Hi Amelia,

    Thank you for contacting Panopto. I am Nedim and I will try to help you with PowerShell script to get AccessToken.

    Here is a script code that you can use:

    # API Endpoint for Token Request
    $tokenEndpoint = "https://Your Site.panopto.com/Panopto/oauth2/connect/token"
    
    
    # Client Credentials
    $clientID = "Client ID"
    $clientSecret = "Client Secret - no need to encode"
    
    
    # User Credentials
    $username = "Panopto admin username"
    $password = "Panopto user password"
    
    
    # Token Request Parameters
    $body = @{
        grant_type = "password"
        client_id = $clientID
        client_secret = $clientSecret
        username = $username
        password = $password
        scope = "api"
    }
    
    
    # Perform Token Request
    $response = Invoke-RestMethod $tokenEndpoint -Method 'POST' -Headers $headers -Body $body
    $response | ConvertTo-Json
    

    I hope this will help you.

    Please let us know if you have additional questions.

    Regards.

    Nedim

    

  • Options

    Thanks Nedim for providing the code example, but it's unfortunately incomplete. What should be in the $headers variable?

  • Options
    Nedim DeliahmetovicNedim Deliahmetovic Panopto Employee

    Hi Amelia,

    You can add following header but it works without header too:

    $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $headers.Add("Content-Type", "application/x-www-form-urlencoded")
    

    Have you tried code without header that I provided in the previous post? Does it work for you without header?

    I hope this will help.

    Regards.

  • Options

    Hi Nedim,

    Thanks for providing header values. Unfortunately, I'm still getting the "invalid_client" error regardless of if I include or exclude the header.

    I've confirmed that the username and password are correct (tested logging in with them), and I also double checked the client ID and secret for the User Based Server Application (the app is enabled and CORS Origin URL is https://penncareylaw.hosted.panopto.com, see attached screenshot).

    Do you have any other ideas for how to resolve this?

    Thank you,

    Amelia


  • Options

    Hi Nedim,

    Thanks - I generated a new API client and was able to get the access token in Postman and with PowerShell. There must have been something wrong with the API client I created.

    Thanks for helping me get connected to the API!

    Amelia

Sign In or Register to comment.