Sample code for 30+ languages & platforms
PowerBuilder

Test Salesforce OAuth2 Access Token

See more Salesforce Examples

Demonstrates how to make a simple Salesforce REST API call to test a previously obtained access token.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Http
oleobject loo_Json
oleobject loo_SbUrl
oleobject loo_SbResponseBody
oleobject loo_JsonResponse

li_Success = 0

// This example does the following:

// curl -X GET https://yourInstance.salesforce.com/services/oauth2/userinfo \
//      -H "Authorization: Bearer YOUR_ACCESS_TOKEN"

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if

// Use the following online tool to generate HTTP code from a CURL command
// Convert a cURL Command to HTTP Source Code

// This example assumes the OAuth2 access token was previously fetched 
// and saved to a file.  See Get SalesForce OAuth2 Access Token via Authorization Flow

loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_Json.LoadFile("qa_data/tokens/_salesforce.json")
if li_Success = 0 then
    Write-Debug "Failed to load OAuth2 access token."
    destroy loo_Http
    destroy loo_Json
    return
end if

// Here's an example of the JSON:

// {
//   "access_token": "00D41000....uLZBpT6",
//   "refresh_token": "5Aep....25xdGgkrV",
//   "signature": "cjTbSc5DvcKpaMoRTzuQTJLb1tcMw8LEO01flq4aMD4=",
//   "scope": "refresh_token id",
//   "instance_url": "https://d41000000f8a0eak-dev-ed.my.salesforce.com",
//   "id": "https://login.salesforce.com/id/00D41000000F8A0EAK/005410000....xAAE",
//   "token_type": "Bearer",
//   "issued_at": "1738348388166"
// }

// Adds the "Authorization: Bearer YOUR_ACCESS_TOKEN" header.
loo_Http.AuthToken = loo_Json.StringOf("access_token")

// We want to build the following URL:  https://<instance_id>.salesforce.com/services/oauth2/userinfo
loo_SbUrl = create oleobject
li_rc = loo_SbUrl.ConnectToNewObject("Chilkat.StringBuilder")

loo_SbUrl.Append(loo_Json.StringOf("instance_url"))
loo_SbUrl.Append("/services/oauth2/userinfo")

loo_SbResponseBody = create oleobject
li_rc = loo_SbResponseBody.ConnectToNewObject("Chilkat.StringBuilder")

li_Success = loo_Http.QuickGetSb(loo_SbUrl.GetAsString(),loo_SbResponseBody)
if li_Success = 0 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_Json
    destroy loo_SbUrl
    destroy loo_SbResponseBody
    return
end if

Write-Debug "Response status code = " + string(loo_Http.LastStatus)

loo_JsonResponse = create oleobject
li_rc = loo_JsonResponse.ConnectToNewObject("Chilkat.JsonObject")

loo_JsonResponse.LoadSb(loo_SbResponseBody)
loo_JsonResponse.EmitCompact = 0
Write-Debug loo_JsonResponse.Emit()

// The expected JSON response is something like this:

// {
//   "sub": "005xxxxxxxxxxxx",
//   "name": "John Doe",
//   "preferred_username": "johndoe@example.com",
//   "email": "johndoe@example.com",
//   "profile": "https://na85.salesforce.com/005xxxxxxxxxxxx"
// ...
// ...
// }


destroy loo_Http
destroy loo_Json
destroy loo_SbUrl
destroy loo_SbResponseBody
destroy loo_JsonResponse