Skip to main content

Quick Tips : Use SFDX Auth token within CURL requests (Mac/ Linux)

I came across a peculiar requirement recently, wherein we needed to download files in a scheduled job. As you all know downloading a file can be a tricky affair with Salesforce. 

Fortunately, using REST API we can achieve it. So, the solution became somewhat easier, wherein we could make a CURL call and download the file. 

However, all usual cases reference using login name/ password or some reference using the Connected app (OAuth) route. For my specific use case, I already have a machine setup with SFDX and with target orgs authenticated.

This solution helps just in that case. With command line scripting it is quite easy (for folks who are experienced with the command line). It took some time for me to get it working.

Note: This approach should be chosen after appropriate review/analysis of other approaches, namely, OAuth-based login flows for app authentication.

Tools used

  1. SFDX - to retrieve auth tokens
  2. Curl - to make API calls via the command line 
  3. JQ - to parse JSON in the command line

Steps 1 - Get Org Info

This command helps retrieve target org's
sfdx force:org:display -u <TAGET ORG> --json

Result (masked information)
{
"status": 0,
"result": {
"username": "anshul.verma@targetorg.com",
"id": "00D3I00000089ASPUAA",
"connectedStatus": "Connected",
"accessToken": "<AUTH TOKEN>",
"instanceUrl": "https://targetorg.my.salesforce.com",
"clientId": "PlatformCLI",
"alias": "targetorg"
}
}

Steps 2 - Extract auth token

This command uses the previous command and extracts the auth token from the JSON response of the previous command

sfdx force:org:display -u <TARGET ORG> --json | jq '.result.accessToken' -r

Note: JQ allows us to simply parse and extract desired value within JSON data

Result
(shown in multi-line command format for rendering here)
anshul@Anshuls-MacBook-Pro ~ % sfdx force:org:display -u sv_newdev --json |
pipe> jq '.result.accessToken' -r

<SECURE TOKEN>


Step 3 - Fire the CURL command to make request

Fire API request by using auth token within CURL

curl "https://<TARGET ORG>.my.salesforce.com/apex/AccountData?id=0013I0000039NTTQA2"
-H "Authorization: Bearer <AUTH TOKEN>"


Final - Combine all of these into one command

curl "https://<TARGET ORG>.my.salesforce.com/apex/testAccountPDF?id=0013I0000039NTTQA2"
-H "Authorization: Bearer $(sfdx force:org:display -u <TARGET ORG> --json | jq '.result.accessToken' -r)"


This is a small one-liner code that allows you to invoke an API (in this particular case, get code of visualforce page).

Comments

Popular posts from this blog

Quick Tips: Salesforce default Images

Well, I'm sure a lot of you still rely on using out of the box salesforce images for displaying quick icons within formula fields or even using them within your Visualforce pages. Lately, I realized that a lot of earlier resources are no longer accessible, so I tried to quickly extract all images from Salesforce CSS files and provide a quick reference here. Please note, I've referenced all images from SF servers directly, so if anything changes, the image should stop rendering here. As these images are completely controlled by Salesforce, and in case they change anything, it might lead to image not being accessible. Image path Image /img/samples/flag_green.gif /img/samples/flag_green.gif /img/samples/flag_red.gif /img/samples/color_red.gif /img/samples/color_yellow.gif /img/samples/color_green.gif /img/samples/light_green.gif /img/samples/light_yellow.gif /img/samples/light_red.gif /img/samples/stars_100.gif /img/samples/stars_200.gif /img/samples/stars_300.

Lightning: Generate PDF from Lightning components with in-memory data

I'm sure as everyone is diving into lightning components development, they are getting acquainted with the nuances of the Lightning components framework. As well as, its current limitations. Being a new framework, this is bound to happen. Although we have our users still using salesforce classic, we have started using lightning components framework our primary development platform and Visualforce is considered primarily for rendering lightning components within Classic Service console. Recently, while re-architecting a critical module, we encountered a problem wherein we needed to generate PDF from lightning components. Now, being Javascript intensive framework, it has limited room for such features (may be included in future roadmap). As of now, there is no native feature within the lightning framework to do so (at least I didn't find anything). Common Scenario - Create Visualforce page to retrieve data and generate PDF For scenarios where the data exist within Sa

Lightning: Generate PDF within Lightning Experience with Salesforce Data

Some time back I posted a solution to generate PDF from Lightning components using in-memory data. Post url: http://www.vermanshul.com/2017/07/lightning-generate-pdf-from-lightning.html It was developed for a specific scenario, wherein we need to generate PDF where: User interface is Salesforce classic Initiated via Lightning Component Data doesn't exist within Salesforce and is completely in-memory As complex and tricky this situation was, we did end up finding a stable and equally tricky solution. However, I realize that there are still lack of solutions (or maybe my search skills are downgrading) to generate and automatically download PDF document from Lightning Experience, without using any lightning components, wherein data exists within Salesforce. You can use the earlier solution in that case, but it will be an overkill. There are various solutions available to generate PDF from javascript. But, I still think the plain old method of converting HTML to PDF (via