PowerShell: How To Invoke Rest Method with RingCentral Rest API

[string]$restApiTokenUrl="https://platform.ringcentral.com/restapi/oauth/token"
[string]$restApiUrl="https://platform.ringcentral.com/restapi/v1.0/account/~/extension?page=1"
[string]$username='15555555555'
[string]$password='PASSWORDHERE'
[string]$extension='100'
[string]$appKey='APPKEYHERE'
# Part 1: Obtain Rest-API Token / Authorization
function obtainRingCentralTokenAuth{
  param(
      [string]$restApiTokenUrl,
      [string]$username,
      [string]$password,
      [string]$extension,
      [string]$appKey
      )
      [hashtable]$postAuthorization = @{
        grant_type = 'password'
        username = $username
        password = $password
        extension = $extension
      }
    $headers=New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $headers.Add("Authorization", "Basic $appKey")
    $headers.Add("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8")
    $token=Invoke-RestMethod -Method Post -Uri $restApiTokenUrl -Body $postAuthorization -headers $headers -ContentType "application/x-www-form-urlencoded"
    $authorization = $token.token_type + " " +$token.Access_token
    return $authorization
}

# Part 2:
function getRecordsViaRestApi{
    param(
        [string]$restApiUrl,
        [string]$authorization
    )
    try{
        $results=Invoke-RestMethod -uri $restApiUrl -Headers @{"Authorization"="$authorization"} -ea Stop
        return $results
    }catch{
        write-warning $_
        return $false    
    }
}

$authorization=obtainRingCentralTokenAuth $restApiTokenUrl $username $password $extension $appKey
getRecordsViaRestApi $restApiUrl $authorization

Leave a Reply

Your email address will not be published. Required fields are marked *