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: Export Event Logs by a Certain Date Range

# User defined variables $daysLimit=7 $startdate=(get-date).adddays(-$daysLimit) $computername=$env:computername $logName='Application' $filterTypes='Error','Warning' $logPath="c:\temp\eventlogs-from-$($startdate.tostring('yyyy-MM-dd'))-to-$((get-date).tostring('yyyy-MM-dd')).csv" # Get the event logs…

Docker DRUPAL Container

# Preseed drupal files with persitent storagemkdir /var/www/html/kimconnect/{modules,profiles,sites,themes}docker run --rm drupal tar -cC /var/www/html/sites .…

Resolve Error: (59) An unexpected network error occurred

Prerequisites:   Ensure the the target machine has PowerShell Remoting Enabled (as it's Disabled by…