Advertisement
Guest User

Untitled

a guest
Jun 24th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #These will need to be updated for prod
  2. $api_server_url = "https://platform.devtest.ringcentral.com"
  3. $media_server_url = "https://media.devtest.ringcentral.com:443"
  4. $username = '<user_name>'
  5. $password = '<password>'
  6. $extension = '<test extension>'
  7.  
  8. #Base64 encoded strings as : Appkey:Appsecurity_key   use this site: https://www.base64encode.org/ or build your own. it never changes so meh
  9. $app_key = "<APP KEY>"
  10.  
  11.  
  12. #
  13. # No config changes needed beyond this point needed
  14. #
  15.  
  16. #Global default header.
  17. #Used for auth_initiate and auth_refresh
  18. $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
  19. $headers.Add("Authorization", "Basic $app_key")
  20. $headers.Add("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8")
  21.  
  22. #Global authorization hashtable. This will be handed through functions/etc to maintain authorization
  23.     $auth_obj = [PSCustomObject] @{
  24.     Isset = $false
  25.     }
  26.    
  27.  
  28. #Called from get-authstatus this creates the initial authorization tokens and populates the global $auth_obj with that data
  29. #It also returns the object for parsing, or False for a failure and logs the error.
  30. function auth_initiate() {
  31.    
  32.     #Authentication post data
  33.     $auth_post = @{
  34.     grant_type = 'password'
  35.     username = $username
  36.     password = $password
  37.     extension = $extension
  38.     }
  39.  
  40.     try {
  41.     $url = $api_server_url + "/restapi/oauth/token"
  42.     $auth_token = invoke-restmethod -Method Post -Uri $url -Body $auth_post -headers $headers -ContentType "application/x-www-form-urlencoded"
  43.     $authorization = $auth_token.token_type + " " +$auth_token.Access_token
  44.     } catch {
  45.     echo "Error refresh token: $_" >> $log_path
  46.     return $False
  47.     }
  48.    
  49.     $Global:auth_obj = [PSCustomObject] @{
  50.     Isset = $true
  51.     authorization = $authorization
  52.     refresh_token = $auth_token.refresh_token
  53.     expires_in = (Get-date).Addseconds($auth_token.expires_in)
  54.     refresh_token_expires_in = (Get-date).Addseconds($auth_token.refresh_token_expires_in)
  55.     scope = $auth_token.scope
  56.     owner_id = $auth_token.owner_id
  57.     endpoint_id = $auth_token.endpoint_id
  58.     }
  59. return $auth_obj
  60. }
  61.  
  62. #Called from get-authstatus this refreshes authorization tokens and populates the global $auth_obj with that data
  63. #It also returns the object for parsing, or False for a failure and logs the error.
  64. function auth_refresh() {
  65.     $refresh_post  = @{
  66.     grant_type = 'refresh_token'
  67.     refresh_token = $refresh_token
  68.     }
  69.  
  70.     $url = $api_server_url + "/restapi/oauth/token"
  71.  
  72.     try {
  73.     $auth_token = invoke-restmethod -Method Post -Uri $url -Body $refresh_post -headers $headers -ContentType "application/x-www-form-urlencoded"
  74.     $authorization = $auth_token.token_type + " " +$auth_token.Access_token
  75.     } catch {
  76.     echo "Error refresh token: $_" >> $log_path
  77.     return $false
  78.     }
  79.    
  80.     $Global:auth_obj = [PSCustomObject] @{
  81.     Isset = $true
  82.     authorization = $authorization
  83.     refresh_token = $auth_token.refresh_token
  84.     expires_in = (Get-date).Addseconds($auth_token.expires_in)
  85.     refresh_token_expires_in = (Get-date).Addseconds($auth_token.refresh_token_expires_in)
  86.     scope = $auth_token.scope
  87.     owner_id = $auth_token.owner_id
  88.     endpoint_id = $auth_token.endpoint_id
  89.     }
  90.    
  91. return $auth_obj
  92. }
  93.  
  94.  
  95. function get-authstatus() {
  96.     if(($auth_obj.expires_in -gt (get-date)) -and ($auth_obj.isset -eq $true)) {
  97.     return $true
  98.     }elseif(($auth_obj.expires_in -gt (get-date)) -and ($auth_obj.isset -eq $true)) {
  99.         if(auth_refresh) {
  100.         echo "Token expired and refreshed successfully" >> $log_path
  101.         return $true
  102.         } else {
  103.         echo "Failed Token refresh" >> $log_path
  104.         return $false
  105.         }
  106.     } else {
  107.         if(auth_initiate) {
  108.         echo "Initializing Auth token">> $log_path
  109.         return $true
  110.         }
  111.     }
  112. }
  113.  
  114.  
  115. #
  116. # Example of making a call to the api
  117. #
  118. Function get-calllog() {
  119. if(get-authstatus) {
  120.     $auth_token = $auth_obj.authorization
  121.     } else {
  122.     return $false
  123.     }
  124. try {
  125. $call = invoke-restmethod -uri "$api_server_url/restapi/v1.0/account/~/call-log"  -Headers @{"Authorization" = "$auth_token"}
  126. } catch {
  127. return $false
  128. }
  129. return $call
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement