question

John Morrow avatar image
John Morrow asked John Morrow commented

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

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

}

rest api
1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Phong Vu avatar image
Phong Vu answered John Morrow commented

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

1 comment
1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

John Morrow avatar image John Morrow commented ·

So replace

1710971761483.png


with

1710971775435.png

???

0 Likes 0 ·
1710971761483.png (27.9 KiB)
1710971775435.png (34.4 KiB)
John Morrow avatar image
John Morrow answered John Morrow commented

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



1710972406222.png (93.3 KiB)
2 comments
1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Phong Vu avatar image Phong Vu ♦♦ commented ·

There is a link to another thread in the one I linked above. So check that out too.

0 Likes 0 ·
John Morrow avatar image John Morrow Phong Vu ♦♦ commented ·

Thank you for the suggestions thus far. I feel like I am very close to getting this to work. Would you kindly look over my script and let me know if you have any suggestions on how to fix? Thank you!


Cannot connect to RC api using powershell - RingCentral Community Forums

0 Likes 0 ·

Developer sandbox tools

Using the RingCentral Phone for Desktop, you can dial or receive test calls, send and receive test SMS or Fax messages in your sandbox environment.

Download RingCentral Phone for Desktop:

Tip: switch to the "sandbox mode" before logging in the app:

  • On MacOS: press "fn + command + f2" keys
  • On Windows: press "Ctrl + F2" keys