Skip to main content
Question

converting my script to use JWT token instead of password-based authentication?

  • 19 March 2024
  • 3 replies
  • 198 views

Let me start off by saying I am not a developer. This script was created by a colleague of mine and currently uses password-authentication.


I have followed the RC guide on how to create a JWT token and I have that but I am having issues getting it to work.


Is there a guide that specifically shows how to use a JWT token with my powershell script out there?


Below is my current script, how would I modify it to use my new JWT token I generated on my RC account? Thanks for any help! Note: I sanitized the code to remove any personal infomation.



function Get-Token {

#Get New Token#
$tokenheaders = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$tokenheaders.Add("Authorization", 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX==')
$tokenheaders.Add("Content-Type", 'application/x-www-form-urlencoded')

$body = 'grant_type=password&username=+19999999990&extension=101&password=XXXXXXXXXXXXXXXXXXXXX^'

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12
$T = Invoke-RestMethod "https://platform.ringcentral.com/restapi/oauth/token" -Headers $tokenheaders -Body $body -Method Post

$Token = $t.access_token

return $Token
}

$Token = Get-Token
##############################################
#API GET Request Header
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Bearer $Token")
$headers.Add("Accept", 'application/json')
$headers.Add("content-type", 'application/json')

#Get Users
$AllUsers = Invoke-RestMethod 'https://platform.ringcentral.com/restapi/v1.0/account/~/extension/?perPage=1000' -Headers $headers -Method Get

$UsersArray = @()
foreach ($id in $AllUsers.records) {

$userinfo = $null
$department = $null
$jobtitle = $null
$emailCheck = $null

$email = $id.contact.email
$userinfo = Get-ADUser -Filter "UserPrincipalName -eq '$email'" -properties department, title
$emailCheck = $userinfo.UserPrincipalName

if($emailcheck -ne $null -and $id.type -ne "Department") {
$department = $userinfo.department
$jobtitle = $userinfo.title

$UsersArray += (,($id.id, $id.contact.email, $department, $jobtitle))
}


#$UsersArray = (,($id.id, "USER@DOMAIN.COM", "Technology", "Mutliple Word Job Title"))
}

foreach ($user in $UsersArray) {
$did = $user[0]

echo $user[1]
#echo $user[3]
#echo $user[2]

$putbody = '{"contact": {"jobTitle":"' + $user[3] + '"}}'
Invoke-RestMethod "https://platform.ringcentral.com/restapi/v1.0/account/~/extension/$did" -Headers $headers -Method PUT -Body $putbody

$putdepartment = '{"contact": {"department":"' + $user[2] + '"}}'
Invoke-RestMethod "https://platform.ringcentral.com/restapi/v1.0/account/~/extension/$did" -Headers $headers -Method PUT -Body $putdepartment

Start-Sleep -Seconds 3

$did = $null

}

3 replies

Userlevel 2
Badge

Check this out and implement it in the Get-Token part in your code.

So replace

1710971761483.png


with

1710971775435.png

???

It looks like it can connect now based off the recommdation from Phong but I think there are still some authentication problems. Anyone know what I can do about this error?


1710972406222.png


Reply