BeginSession

Description

Creates a session in the installable cloud application deployed with PowerServer.

This function is only effective in the installable cloud application deployed with PowerServer.

Applies to

Application object

Syntax

applicationname.BeginSession ( )

Argument

Description

applicationname

The name of the application object for which you want to create a session


Usage

Normally there is no need to call this function to create a session, because the session is automatically created when the application starts. You may want to execute this function to create a session, only when you have obtained the access token and you want the session to include the access token, or when you have dynamically modified the Web API URL for the application via SetPowerServerURL.

In order to execute this function successfully, the "Dynamic session parameters" option must be selected in the Application painter's Properties view so that the session will not be created automatically, otherwise, this function will return -1 (if the session has already been created automatically).

You can only call this function one time to create one session after the application started; once a session was created successfully by this function, do not call this function again unless the application is re-started, otherwise, this function will return -1 (as session already exists).

Return value

Integer.

Returns 0 if it succeeds.

Returns -1 if one of the following happens:

  • the session has already been created automatically.

  • the BeginSession function is called more than one time when the session is still valid.

  • the session is invalid because it has timed out or the Web APIs has re-started.

  • the current application is not an installable cloud app deployed with PowerServer.

Returns one of the following numbers if an error occurs:

  • 220 -- General session error.

  • 222 -- Cannot connect with the server when creating the session.

  • 230 -- License errors.

  • 240 -- Unauthorized.

Examples

Long ll_Return
String ls_TokenType, ls_AccessToken, ls_Json
JsonParser ljson_Parser

ljson_Parser = Create JsonParser

ls_Json = '{' +&
'    "access_token": "eyJhbGciOiJSUzI1NiIsImtpZCI6IjM4OEQ0NEIxRDdBNkVFOUQxNTU0MDAzQUMzM0QzQzkzIiwidHlwIjoiYXQrand0In0.eyJuYmYiOjE2MTI0MDIxODUsImV4cCI6MTYxMjQwMjM2NSwiaXNzIjoiaHR0cHM6Ly9sb2NhbGhvc3Q6NTAwMSIsImNsaWVudF9pZCI6IllvdXJDbGllbnRJZFRoYXRDYW5Pbmx5UmVhZCIsInN1YiI6IlBBTkdDSFVHVUFOIiwiYXV0aF90aW1lIjoxNjEyNDAyMTg1LCJpZHAiOiJsb2NhbCIsImp0aSI6IjFDNEQ4NEFBODU0REYxODMwQkVGQTAwQTFBMjdFMDk0IiwiaWF0IjoxNjEyNDAyMTg1LCJzY29wZSI6WyJzY29wZS5yZWFkYWNjZXNzIl0sImFtciI6WyJjdXN0b20iXX0.DbJsip696O1HlTFjAmayohhHGEcjIQxJM7JICi9ho5MAv2Av4YjQOGgXrCNFhwqG4AFTf3hHwufGYXxzXI8tCYfn3F3ro36dd-0h_A-__5sBelZD0vr6nmWh8Uy-zoJenzsrnVEv8VHVNi2enuKH6SBe-Q59bn5A-Gbg6viNjv0S5HIaKuWeawExBH6uEAu28RlSlfG2bwWuCsyhYWOPIhNhYcZTbVD8ks0SFodGeGS-ZqNOVrOTKb9oqnhvYxnuNofJpjToZ8ZZdKyWiyJUhItpTQTyI3MsCHoVU4X101YTPrVMF_-OuPSwXm6QMl3QtI_6Hy_PeYHL-xz2-q3tog",' +&
'    "expires_in": 3600,' +&
'    "token_type": "Bearer",' +&
'    "scope": "scope.readaccess"' +&
'}'

ljson_Parser.LoadString( ls_Json )
ls_TokenType = ljson_Parser.GetItemString( "/token_type" )
ls_AccessToken = ljson_Parser.GetItemString( "/access_token" )
ll_Return = Getapplication().SetHttpRequestHeader( "Authorization", ls_TokenType + " " + ls_AccessToken, True )

Try
 ll_return = Getapplication().Beginsession()
 If ll_return <> 0 Then
  Messagebox("Beginsession Failed: " + String(ll_return), Getapplication().GetHttpResponseStatusText())
 End if
Catch ( Throwable ex)
 MessageBox( "Throwable", ex.GetMessage())
End Try

If IsValid ( ljson_Parser ) Then Destroy ( ljson_Parser )

See also

GetPowerServerURL

SetPowerServerURL

GetHttpResponseHeaders

GetHttpResponseStatusCode

GetHttpResponseStatusText

SetHttpRequestHeader