Using Powershell with the RingCentral API

I had the opportunity to work with the RingCentral API. I ended up with a series of functions that will handle the authorization/token information for you as long as you don’t mind using the Password authorization work flow. [When getting the API key](https://developer.ringcentral.com/library/getting-started.html). Make sure the “Platform Type” is set to Desktop (Mac/Windows/Other).

Code covered in this post can be found on pastebin or on my github
Quick Review of the Process and API:

2) A total of 20 calls must be made and each permission must be used at least once.

3) Out of all calls made over 48 hours no more than 5% can generate errors (getting throttled, bad calls, etc.)

4) It can take up to 7 days for your app to be approved.

Much of the detection is automatic, but it can take several hours for it to pickup your usage and errors. None of the requirements from the sandbox app to the production app are unreasonable but I found the process a bit clunky. Another interesting quirk is not all permissions for your app can be requested from the portal. You’ll need to contact email support for something like downloading call recordings. The email support is pretty responsive and I was pleased with that. I do wish the sandbox environment came with bogus data already populated. Rather than explore and get ideas what I can do with the API I kept spending time building fake data to resemble the production environment. Not horrible, but something that could be a bit more friendly.

The API documentation lacked any examples for Powershell. Though they didn’t have any examples, the documentation is generalized and written well enough that it wasn’t show stopping to figure out how to get a few examples done.

Before I go into the authorization scripts I want to focus on the global variables and configuration.

#Base64 encoded strings as : Appkey:Appsecurity_key use this site: https://www.base64encode.org/ or build your own. it never changes so meh
$app_key = “”

$log_path = “C:\scripts\log\ringcentral.log”

All of the information can be found at https://service.devtest.ringcentral.com and https://developer.ringcentral.com/library/tutorials/get-started.html . The $app_key I didn’t bother to do dynamically since it is set and forget. You can go to most websites and get it encoded.
Make sure your $log_path is pointed to the log file.

You can test if everything is working by running get-authstatus.
If it returns true you’re ready to start running API calls. If you get false check the $log_path file and check your authorization information for typos.

If you want to use the auth functions in your own scripts you only really need a small chunk of code:
if(get-authstatus) {
$auth_token = $auth_obj.authorization
} else {
return $false
}

This function checks that the initial authorization has been done, and none of the tokens have expired. If they have expired, it calls the correct function to initialize the authorization or to renew it.

The above function creates the initial authorization token, and changes the time the token expires into the local system time. It does the same for the refresh token’s expiration as well. All of that information is populated into the authorization global object.

2 thoughts on “Using Powershell with the RingCentral API”

I think I ran into this and it had to do with the wrong “Platform Type” when setting up the API keys. Make sure you are set to Desktop (Mac/Windows/Other) .
If that doesn’t sort it out let me know. I was looking through and I think I failed to mention that. I’ll update the blog with the information.