![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PureBasic) Test Salesforce OAuth2 Access TokenSee more Salesforce ExamplesDemonstrates how to make a simple Salesforce REST API call to test a previously obtained access token.
IncludeFile "CkHttp.pb" IncludeFile "CkStringBuilder.pb" IncludeFile "CkJsonObject.pb" Procedure ChilkatExample() ; This example does the following: ; curl -X GET https://yourInstance.salesforce.com/services/oauth2/userinfo \ ; -H "Authorization: Bearer YOUR_ACCESS_TOKEN" http.i = CkHttp::ckCreate() If http.i = 0 Debug "Failed to create object." ProcedureReturn EndIf ; 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 json.i = CkJsonObject::ckCreate() If json.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success.i = CkJsonObject::ckLoadFile(json,"qa_data/tokens/_salesforce.json") If success = 0 Debug "Failed to load OAuth2 access token." CkHttp::ckDispose(http) CkJsonObject::ckDispose(json) ProcedureReturn EndIf ; 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. CkHttp::setCkAuthToken(http, CkJsonObject::ckStringOf(json,"access_token")) ; We want to build the following URL: https://<instance_id>.salesforce.com/services/oauth2/userinfo sbUrl.i = CkStringBuilder::ckCreate() If sbUrl.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkStringBuilder::ckAppend(sbUrl,CkJsonObject::ckStringOf(json,"instance_url")) CkStringBuilder::ckAppend(sbUrl,"/services/oauth2/userinfo") sbResponseBody.i = CkStringBuilder::ckCreate() If sbResponseBody.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success = CkHttp::ckQuickGetSb(http,CkStringBuilder::ckGetAsString(sbUrl),sbResponseBody) If success = 0 Debug CkHttp::ckLastErrorText(http) CkHttp::ckDispose(http) CkJsonObject::ckDispose(json) CkStringBuilder::ckDispose(sbUrl) CkStringBuilder::ckDispose(sbResponseBody) ProcedureReturn EndIf Debug "Response status code = " + Str(CkHttp::ckLastStatus(http)) jsonResponse.i = CkJsonObject::ckCreate() If jsonResponse.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkJsonObject::ckLoadSb(jsonResponse,sbResponseBody) CkJsonObject::setCkEmitCompact(jsonResponse, 0) Debug CkJsonObject::ckEmit(jsonResponse) ; 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" ; ... ; ... ; } CkHttp::ckDispose(http) CkJsonObject::ckDispose(json) CkStringBuilder::ckDispose(sbUrl) CkStringBuilder::ckDispose(sbResponseBody) CkJsonObject::ckDispose(jsonResponse) ProcedureReturn EndProcedure |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.