Sample code for 30+ languages & platforms
Chilkat2-Python

Configure HTTP Basic Authentication with SecureString

See more REST Examples

Demonstrates Rest.SetAuthBasicSecure, which configures HTTP Basic authentication using SecureString objects instead of plaintext strings.

Background. A SecureString keeps sensitive values encrypted in memory, reducing the window in which credentials appear as readable plaintext. This is otherwise equivalent to SetAuthBasic.

Chilkat Chilkat2-Python Downloads

Chilkat2-Python
import sys
import chilkat2

success = False

rest = chilkat2.Rest()
bTls = True
bAutoReconnect = True
success = rest.Connect("example.com",443,bTls,bAutoReconnect)
if (success == False):
    print(rest.LastErrorText)
    sys.exit()

# SetAuthBasicSecure configures HTTP Basic authentication using SecureString objects, which keep
# the credentials encrypted in memory.
# Normally you would not hard-code secrets in source.  You should instead obtain them from an
# interactive prompt, environment variable, or a secrets vault.
ssUsername = chilkat2.SecureString()
ssUsername.Append("myUsername")
ssPassword = chilkat2.SecureString()
ssPassword.Append("myPassword")

success = rest.SetAuthBasicSecure(ssUsername,ssPassword)
if (success == False):
    print(rest.LastErrorText)
    sys.exit()

responseText = rest.FullRequestNoBody("GET","/api/protected")
if (rest.LastMethodSuccess == False):
    print(rest.LastErrorText)
    sys.exit()

print(responseText)