Advertisement
Guest User

Untitled

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