Sample code for 30+ languages & platforms
PowerBuilder

Configure OAuth 2.0 Authentication for REST

See more REST Examples

Demonstrates Rest.SetAuthOAuth2, which associates an OAuth2 provider containing a valid access token so Chilkat adds the bearer token to the Authorization header.

Background. OAuth 2.0 bearer-token authentication attaches the access token to each request. The token is typically obtained separately through an OAuth 2.0 authorization flow before being assigned to the provider.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Rest
integer li_BTls
integer li_BAutoReconnect
oleobject loo_Oauth2
string ls_ResponseText

li_Success = 0

loo_Rest = create oleobject
li_rc = loo_Rest.ConnectToNewObject("Chilkat.Rest")
if li_rc < 0 then
    destroy loo_Rest
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_BTls = 1
li_BAutoReconnect = 1
li_Success = loo_Rest.Connect("example.com",443,li_BTls,li_BAutoReconnect)
if li_Success = 0 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    return
end if

//  Configure OAuth 2.0 bearer-token authentication.  The provider must contain a valid access token.
loo_Oauth2 = create oleobject
li_rc = loo_Oauth2.ConnectToNewObject("Chilkat.OAuth2")

//  Normally you would not hard-code secrets in source.  You should instead obtain them from an
//  interactive prompt, environment variable, or a secrets vault.
loo_Oauth2.AccessToken = "OAUTH2_ACCESS_TOKEN"

//  Associate the provider so Chilkat adds the bearer token to the Authorization header.
li_Success = loo_Rest.SetAuthOAuth2(loo_Oauth2)
if li_Success = 0 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    destroy loo_Oauth2
    return
end if

ls_ResponseText = loo_Rest.FullRequestNoBody("GET","/api/resource")
if loo_Rest.LastMethodSuccess = 0 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    destroy loo_Oauth2
    return
end if

Write-Debug ls_ResponseText


destroy loo_Rest
destroy loo_Oauth2