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

Basic HTML and HTML5: Nest Many Elements within a Single div Element

<h2>CatPhotoApp</h2><main><p>Click here to view more <a href="#">cat photos</a>.</p><a href="#"><img src="https://bit.ly/fcc-relaxing-cat" alt="A cute orange cat lying…

PowerShell: Creating VSS Snapshots on Microsoft File Server Clusters

<# VSS-Snapshots-on-Microsoft-File-Server-Clusters_v0.0.1.ps1 # Author: KimConnect.com Functions: 1. Create snapshots of all volumes on a file…

PowerShell: Detecting Windows Antivirus

One of the initial tasks of a Windows user is to determine whether a computer…