converting my script to use JWT token instead of password-based authentication?
Tags: rest api
Mar 19, 2024 at 8:36am • 2 replies • 0 likes
John Morrow
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 }
2 Answers
Check this out and implement it in the Get-Token part in your code.
0
John Morrow on Mar 20, 2024 at 2:56pm • 0 likes
So replace
with
???
A new Community is coming to RingCentral!
Posts are currently read-only as we transition into our new platform.We thank you for your patience
during this downtime.
Try Workflow Builder
Did you know you can easily automate tasks like responding to SMS, team messages, and more? Plus it's included with RingCentral Video and RingEX plans!Try RingCentral Workflow Builder