question

John Morrow avatar image
John Morrow asked Phong Vu commented

Cannot connect to RC api using powershell

Cannot connect to API using my powershell script. It looks like it connects but then when it starts running through the AD portion of my script it errors out with this error:


t C:\PSAutomations\RCUpdateInfoV3-test.ps1:36 char:17
+ ... $userinfo = Get-ADUser -Filter "UserPrincipalName -eq '$email'" -prop ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-ADUser], ADException
    + FullyQualifiedErrorId : ActiveDirectoryServer:8254,Microsoft.ActiveDirectory.Management.Commands.GetADUser
 
Invoke-RestMethod : {
  "message": "Need Content-Type header"


Full Script:


$RC_SERVER_URL="https://platform.ringcentral.com"
$RC_CLIENT_ID="Yxxxxxx"
$RC_CLIENT_SECRET="xxxxxx"
$RC_JWT_TOKEN = "xxxxxxxx"

$headers = @{
    ContentType = 'application/x-www-form-urlencoded; charset=UTF-8'
    Accept = 'application/json'
    Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("${RC_CLIENT_ID}:${RC_CLIENT_SECRET}"))
}

# RC authenticate and obtain access token
$response = Invoke-RestMethod -Method POST -Uri ($RC_SERVER_URL + "/restapi/oauth/token") -Headers $headers -Body @{
    grant_type = 'urn:ietf:params:oauth:grant-type:jwt-bearer'
    assertion = $RC_JWT_TOKEN
}

# Extyract Access Token
$access_token = $response.access_token

# Fetch User info from RC api
$AllUsers = Invoke-RestMethod ($RC_SERVER_URL + "/restapi/v1.0/account/~/extension/?perPage=1000") -Headers @{
    Authorization = "Bearer $access_token"
} -Method Get

# Array to store users with AD information
$UsersArray = @()

#import AD Module
Import-Module ActiveDirectory

# Iterating over user records
foreach ($id in $AllUsers.records) {
    $email = $id.contact.email
    # Retrieving AD user information using 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
                email = $id.contact.email
                department = $department
                jobtitle = $jobtitle
            }
        )
    } 
}

# Iterating over users and updating profiles
foreach ($user in $UsersArray) {
    $did = $user.id
    $putbody = '{"contact": {"jobTitle":"' + $user.jobtitle + '"}}'
    Invoke-RestMethod ($RC_SERVER_URL + "/restapi/v1.0/account/~/extension/$did") -Headers @{
        Authorization = "Bearer $access_token"
        ContentType = 'application/json'  # Add Content-Type header
    } -Method PUT -Body $putbody
    
    $putdepartment = '{"contact": {"department":"' + $user.department + '"}}'
    Invoke-RestMethod ($RC_SERVER_URL + "/restapi/v1.0/account/~/extension/$did") -Headers @{
        Authorization = "Bearer $access_token"
        ContentType = 'application/json'  # Add Content-Type header
    } -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.

1 Answer

Phong Vu avatar image
Phong Vu answered Phong Vu commented

Try add the Content-Type and the Accept headers to each API request.

# Fetch User info from RC api
$AllUsers = Invoke-RestMethod ($RC_SERVER_URL + "/restapi/v1.0/account/~/extension/?perPage=1000") -Headers @{
    Content-Type = "application/json"
    Accept = "application/json"
    Authorization = "Bearer $access_token"
} -Method Get

5 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.

John Morrow avatar image John Morrow commented ·

Still getting same error after changing the code


below is an excerpt from the code with the changes you suggested.


# RC authenticate and obtain access token
$response = Invoke-RestMethod -Method POST -Uri ($RC_SERVER_URL + "/restapi/oauth/token") -Headers $headers -Body @{
    grant_type = 'urn:ietf:params:oauth:grant-type:jwt-bearer'
    assertion = $RC_JWT_TOKEN
}

# Extyract Access Token
$access_token = $response.access_token

# Fetch User info from RC api
$AllUsers = Invoke-RestMethod ($RC_SERVER_URL + "/restapi/v1.0/account/~/extension/?perPage=1000") -Headers @{
    "Content-Type" = "application/json"
    "Accept" = "application/json"
    "Authorization" = "Bearer $access_token"
} -Method Get
0 Likes 0 ·
John Morrow avatar image John Morrow John Morrow commented ·
At C:\PSAutomations\RCUpdateInfoV3-test.ps1:60 char:5
+     Invoke-RestMethod ($RC_SERVER_URL + "/restapi/v1.0/account/~/exte ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
Invoke-RestMethod : {
  "message": "Need Content-Type header"
0 Likes 0 ·
Phong Vu avatar image Phong Vu ♦♦ John Morrow commented ·

Maybe you can try this

# Fetch User info from RC api
$AllUsers = Invoke-RestMethod ($RC_SERVER_URL + "/restapi/v1.0/account/~/extension/?perPage=1000") -Headers @{
    "Authorization" = "Bearer $access_token"
} -ContentType 'application/json' -Method Get

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

Same issue again.... I am about to give up on Powershell and try this with Python. Would you have any suggestions on how to connect with JWT token using python?

0 Likes 0 ·
Show more comments

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