DataFlex
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoHttp
Handle hoJson
Handle hoSbUrl
Variant vSbResponseBody
Handle hoSbResponseBody
Handle hoJsonResponse
String sTemp1
Integer iTemp1
Move False To iSuccess
// This example does the following:
// curl -X GET https://yourInstance.salesforce.com/services/oauth2/userinfo \
// -H "Authorization: Bearer YOUR_ACCESS_TOKEN"
Get Create (RefClass(cComChilkatHttp)) To hoHttp
If (Not(IsComObjectCreated(hoHttp))) Begin
Send CreateComObject of hoHttp
End
// 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
Get Create (RefClass(cComChilkatJsonObject)) To hoJson
If (Not(IsComObjectCreated(hoJson))) Begin
Send CreateComObject of hoJson
End
Get ComLoadFile Of hoJson "qa_data/tokens/_salesforce.json" To iSuccess
If (iSuccess = False) Begin
Showln "Failed to load OAuth2 access token."
Procedure_Return
End
// 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.
Get ComStringOf Of hoJson "access_token" To sTemp1
Set ComAuthToken Of hoHttp To sTemp1
// We want to build the following URL: https://<instance_id>.salesforce.com/services/oauth2/userinfo
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbUrl
If (Not(IsComObjectCreated(hoSbUrl))) Begin
Send CreateComObject of hoSbUrl
End
Get ComStringOf Of hoJson "instance_url" To sTemp1
Get ComAppend Of hoSbUrl sTemp1 To iSuccess
Get ComAppend Of hoSbUrl "/services/oauth2/userinfo" To iSuccess
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbResponseBody
If (Not(IsComObjectCreated(hoSbResponseBody))) Begin
Send CreateComObject of hoSbResponseBody
End
Get ComGetAsString Of hoSbUrl To sTemp1
Get pvComObject of hoSbResponseBody to vSbResponseBody
Get ComQuickGetSb Of hoHttp sTemp1 vSbResponseBody To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoHttp To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComLastStatus Of hoHttp To iTemp1
Showln "Response status code = " iTemp1
Get Create (RefClass(cComChilkatJsonObject)) To hoJsonResponse
If (Not(IsComObjectCreated(hoJsonResponse))) Begin
Send CreateComObject of hoJsonResponse
End
Get pvComObject of hoSbResponseBody to vSbResponseBody
Get ComLoadSb Of hoJsonResponse vSbResponseBody To iSuccess
Set ComEmitCompact Of hoJsonResponse To False
Get ComEmit Of hoJsonResponse To sTemp1
Showln sTemp1
// 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"
// ...
// ...
// }
End_Procedure