Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Updated Powershell example which was modified by someone and broken
Content layer
id1044102087
Content column
id1044102090
Content block
id1044102088

When using cURL, the user’s credential (user name/password or a token) can be provided at the command line argument. A new session is created during the authentication. It is very important to note that a TWC session remains open after a REST API returns. This implies that a TWC session is not closed automatically. If the user’s credential is still in use (without sending a valid cookie), the license count will be used up, and new sessions cannot be created. The user needs to wait until the session is timed out. The default timeout is 15 minutes.

The correct way to use REST API is to log in only once, reuse the session for all subsequent calls, and log out when finished. The following example is a Bash script used for creating users.

Code Block
languagetext
#!/bin/sh
SERVER=127.0.0.1
RESTBASEURL=https://$SERVER:8111/osmc
ADDUSERURL=${RESTBASEURL}/admin/users
LOGINURL=${RESTBASEURL}/login
LOGOUTURL=${RESTBASEURL}/logout
username=$1
password=$2

curl -v -s -c cookie.txt --insecure -u $username:$password
$LOGINURL

for i in $(seq 1 5) ; do

sed s/@username@/%username%/g
user.js | curl -v -H "Content-Type: application/json" -X POST -s --cookie cookie.txt --insecure -d @- $ADDUSERURL
done

curl -v -s --cookie cookie.txt $LOGOUTURL

The first execution of cURL logs in to the server. The server returns a session ID in a cookie. The cookie is saved into the cookie.txt file. The remaining cURL calls send the cookie back to the server to reuse the open session.

The example below shows how to use PowerShell to create elementsinernal users from a CSV file.

Code Block
languagetext
# -----------------------------------------------------------------
# Script to import users from CSV file into Teamwork Cloud
# Author:  Benjamin Krajmalnik
# Format: non-quoted CSV, no headers
#  userLogin,userPassword,fullUserName,departmentName,emailAddress
#
# Usage:
# PowerShell.exe -ExecutionPolicy Bypass -File
 ImportUsersFromCSV.ps1
# -----------------------------------------------------------------
Add-Type @"
	  using System.Net;
	  using System.Security.Cryptography.X509Certificates;
	  public class TrustAllCertificatesPolicy : ICertificatePolicy 
	  {
    	 public bool CheckValidationResult
		(
			     (
       ServicePoint srvPoint, X509Certificate certificate
			       ,
       		WebRequest request, int certificateProblem
		)
		{
			     ) 
     {
       return true;
		     }
  	}
"@

[System.Net.ServicePointManager]::CheckCertificateRevocationList = $false;
[System.Net.ServicePointManager]::ServerCertificateValidationCallback += { $true; };
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertificatesPolicy
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
$Server = Read-Host -Prompt 'Input the Teamwork Cloud Server Name or IP address'
$Admin = Read-Host -Prompt 'Input the Teamwork Cloud Administrator User'
$Passwd = Read-Host -Prompt 'Input the Teamwork Cloud Server Administrator Password'
$Userfile = Read-Host -Prompt 'Input the full path name of the file containing the users'
$RESTBASEURL = "https://"+$Server+":8111/osmc/"
$ADDUSERURL = $RESTBASEURL + “admin"admin/users"
$LOGINURL = $RESTBASEURL   + “login”"login"
$LOGOUTURL = $RESTBASEURL + “logout”"logout"
$pair="${Admin}($Admin):${Passwd}($Passwd)"
$bytes = [System.Text.Encoding]::ASCII.GetBytes($pair)
$base64 = [System.Convert]::ToBase64String($bytes)
$basicAuthValue = "Basic $base64"
$headers = @{ Authorization = $basicAuthValue }
$RestError = $null
Try {
$response = Invoke-WebRequest -Uri "$LOGINURL" -Method Post -Headers $headers -SessionVariable ‘Session’ 'Session'
} Catch {
	$RestError = $_
}
foreach($line in [System.IO.File]::ReadLines($Userfile))
{
	                  $login,$password,$username,$department,$email = $line.split(',').trim()

 	                  
                  $JSON=@"
              	{
    {
                                            	 "userName": "$login",
     	                                        "password": "$password",
		                                             "otherAttributes": 
		{
			{
                                                            "mobile": "",
                                             		               "name": "$username",
	  		                                                            "department": "$department",
                             		                               "email": "$email"
               	},
                              },
                      	`"enabled`                       "enabled": true
                             	 }
	"@
	                              $response = Invoke-WebRequest  -Uri "$ADDUSERURL" -Method Post -Body $JSON -ContentType "application/json"  -WebSession $Session
}
 
 $response = Invoke-WebRequest  -Uri "$LOGOUTURL" -WebSession $SessionModel Manipulation$Session

Content block
id1044102086

Related pages