Posted On February 5, 2021

PowerShell: How To Invoke Rest Method with RingCentral Rest API

kimconnect 0 comments
blog.KimConnect.com >> Codes >> 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 *

Related Post

PowerShell: removing elements of an immutable “fixed size” array

By default, PowerShell's Array is an object class; hence, its length a "fixed size" as…

Import A WMWare.PowerCLI from Behind the Proxy

VMWare PowerCLI is a must for Administrators. Here's a quick script to install it. If…

PowerShell: Moving Virtual Machines & Expanding Disk Volumes in Hyper-V & Microsoft Failover Clusters

Sample VM Migation Plan (time window = 3 hours): Pre-emptively resolve disks merging errors prior…