Salesforce.com API with cURL

How to play with the salesforce.com api with cURL:

Using https://brew.sh/ or however you do it, install

  • curl
  • jq

Signup for a “Developer Edition” account on https://developer.salesforce.com/page/Developer_Edition and confirm your email. Login.

On the left-hand menu, click “Apps”, “App Manager” and then on the top right side of the screen click “New Connected App”

Fill in the required fields, for the callback URL you can just use https://localhost:8443/demo/callback. This does not need to be a valid URL, we just need to enter something so we can see what the Salesforce API is sending us.

Creating connected app
Creating connected app

When that is complete, you are ready to get your settings. You do not need the app secret here, only the client id.

Get app information
Get app information

Next, you create the following URL with your app information:

https://login.salesforce.com/services/oauth2/authorize?response_type=token&client_id=___YOUR_CLIENT_ID___&redirect_uri=https%3A%2F%2Flocalhost%3A8443%2FOauthDemo%2Foauth%2F_callback

Note that the redirect_uri should be the same one that you used above but encoded as a URI component. If you used a different URI for some reason, use the tool here to get your redirect_uri value: http://pressbin.com/tools/urlencode_urldecode/

So, after doing that copy and paste that URL into your browser window, you should get a salesforce login page. Once you login, salesforce will ask you to confirm that you want to allow access to the application. Click “Allow” and you will be redirected to your “redirect_uri”. In this case, you will get a browser error saying that the page is not available. This is good! Copy the address from your address bar and paste it into a text editor to look at it. It is something like this:

https://localhost:8443/OauthDemo/oauth/_callback#access_token=00Df4000003knBs%21AREAQBBOLGAEnstsVsHi5wKmUfsYpeO26VkWzvleJZBz_Y0Z1zXTiC5N93gEURiTUIA_wHBvGCIALYM8Wp1fISQcoIe3UmPG&instance_url=https%3A%2F%2Fna59.salesforce.com&id=https%3A%2F%2Flogin.salesforce.com%2Fid%2F00Df4000003knBsEAI%2F005f4000001jsGeAAI&issued_at=1526562578724&signature=BoVWC%2B22ewQNAMAt7t4JfmWwIL7%2FPUZZR8sBeXGPgSY%3D&scope=id+api&token_type=Bearer

We are interested in 2 parts of this, the access_token and the instance_url

access_token=00Df4000003knBs%21AREAQBBOLGAEnstsVsHi5wKmUfsYpeO26VkWzvleJZBz_Y0Z1zXTiC5N93gEURiTUIA_wHBvGCIALYM8Wp1fISQcoIe3UmPG

instance_url=https%3A%2F%2Fna59.salesforce.com

Both of these need to be URL decoded (use http://pressbin.com/tools/urlencode_urldecode/ if you need to), and you’ll get:

https://naXY.salesforce.com/ (XY will be a number, like 59)

and something similar to (note the ! character, this is the decoded value):

00Df4000003knBs!AasdfasdfOLGAEnstsVsHi5wKmUfsYpeO26VkWzvleJZBz_Y0Z1zXTsdf1345a3gEURiTUIA_wHBvGCIALYM8Wp1asdf

Now we’re ready to query the API. To verify you have a good instance_url, you can see what services are available by running:

curl https://na59.salesforce.com/services/data/

The result of this us JSON, but ugly:

[{“label”:”Winter ’11”,”url”:”/services/data/v20.0″,”version”:”20.0″},{“label”:”Spring ’11”,”url”:”/services/data/v21.0″,”version”:”21.0″},{“label”:”Summer ’11”,”url”:”/services/data/v22.0″,”version”:”22.0″},{“label”:”Winter ’12”,”url”:”/services/data/v23.0″,”version”:”23.0″},{“label”:”Spring ’12”,”url”:”/services/data/v24.0″,”version”:”24.0″},{“label”:”Summer ’12”,”url”:”/services/data/v25.0″,”version”:”25.0″},{“label”:”Winter ’13”,”url”:”/services/data/v26.0″,”version”:”26.0″},{“label”:”Spring ’13”,”url”:”/services/data/v27.0″,”version”:”27.0″},{“label”:”Summer ’13”,”url”:”/services/data/v28.0″,”version”:”28.0″},{“label”:”Winter ’14”,”url”:”/services/data/v29.0″,”version”:”29.0″},{“label”:”Spring ’14”,”url”:”/services/data/v30.0″,”version”:”30.0″},{“label”:”Summer ’14”,”url”:”/services/data/v31.0″,”version”:”31.0″},{“label”:”Winter ’15”,”url”:”/services/data/v32.0″,”version”:”32.0″},{“label”:”Spring ’15”,”url”:”/services/data/v33.0″,”version”:”33.0″},{“label”:”Summer ’15”,”url”:”/services/data/v34.0″,”version”:”34.0″},{“label”:”Winter ’16”,”url”:”/services/data/v35.0″,”version”:”35.0″},{“label”:”Spring ’16”,”url”:”/services/data/v36.0″,”version”:”36.0″},{“label”:”Summer ’16”,”url”:”/services/data/v37.0″,”version”:”37.0″},{“label”:”Winter ’17”,”url”:”/services/data/v38.0″,”version”:”38.0″},{“label”:”Spring ’17”,”url”:”/services/data/v39.0″,”version”:”39.0″},{“label”:”Summer ’17”,”url”:”/services/data/v40.0″,”version”:”40.0″},{“label”:”Winter ’18”,”url”:”/services/data/v41.0″,”version”:”41.0″},{“label”:”Spring ’18″,”url”:”/services/data/v42.0″,”version”:”42.0″}]

That’s where jq comes in handy. Pipe the cURL output to jq and you’ll run this command:

curl https://na59.salesforce.com/services/data/ | jq

and get this result:

This shows you all the versions available and where they are located. Given the instance_url we have and the url for the version you want, you have your API access base url.

We’ll use version v37.0, so our url is:

curl https://na59.salesforce.com/services/data/v37.0/

Now, when you access the API you need to pass in your bearer token. You do this by specifying a header that is “Authorization: Bearer ${TOKEN}” where ${TOKEN} is your bearer token from above (the thing with the ! in it).

To make this easier, since I’m in a Mac shell, I set the token as a variable I can reference:

TOKEN=”00Df4000003knBs!AasdfasdfOLGAEnstsVsHi5wKmUfsYpeO26VkWzvleJZBz_Y0Z1zXTsdf1345a3gEURiTUIA_wHBvGCIALYM8Wp1asdf”

Now, I can run the following to query the API and pass my authentication token in:

curl https://na59.salesforce.com/services/data/v37.0/ -H “Authorization: Bearer ${TOKEN}”

If you want to do this without the variable, you can just do:

curl https://na59.salesforce.com/services/data/v37.0/ -H “Authorization: Bearer 00Df4000003knBs!AasdfasdfOLGAEnstsVsHi5wKmUfsYpeO26VkWzvleJZBz_Y0Z1zXTsdf1345a3gEURiTUIA_wHBvGCIALYM8Wp1asdf”

If you’ve done it right, you’ll get more endpoints for all the services that this API provides

At this point, you can explore the API and query anything you’d like. The documentation from salesforce.com is here: https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_user_tasks.htm and you can use the examples in this guide along with cURL and your access token to explore.

React Native with Facebook Login

This is done on a Mac and tested with XCode and IOS simulator. I have not gotten around to the Android version yet…

How to create a Facebook login button for React Native app on IOS.

Start by installing react-native via NPM:


npm install -g react-native

Then, change into your projects directory and run the following to create your project:


react-native init babiesdigest_native
cd babiesdigest_native
open ios/babiesdigest_native.xcodeproj

In a browser, open https://developers.facebook.com and select your app – Click “Quickstart” under the products section and note that we will be doing steps 1-6 here

Once XCode is open, click the project in the menu on the left and select General. Set the project identity and the signing team.

Right click project name and click “Add Group”, rename to “Frameworks”

Drag 4 .framework files from FacebookSDK (https://developers.facebook.com) into new “Frameworks” group:

* Bolts.framework
* FBSDKCoreKit.framework
* FBSDKLoginKit.framework
* FBSDKShareKit.framework

Add FacebookSDK directory to search path under project “Build Settings”

In the developers.facebook.com site, add your bundle id from XCode

In Faceobook, enable single sign-on

Copy Facebook’s PList code and paste into XCode

Make AppDelegate.m changes:

For the first block, you must paste the import at the top of the file with the rest of them, and add the
code as follows:

For the second block, add the method below the first method before the @end:

In your terminal, run the bundler app with npm start

Click the run button in XCode to start your simulator, and you should see the app:

Open App.js and import the Facebook classes we’ll use:

Add Button to the list of imports from react-native:

Add a button to the UI in App.js:

Reload the simulator:

Click the login button:

This launches the FB login/confirmation:

Once you login, you get the alert and the stringifyied object in an alert

Migrate WordPress to Docker WordPress

I had several problems when migrating wordpress to Docker wordpress.

  • Blank white page
  • Infinite redirect on wp-admin login page
  • Need to update wp-config.php file for ssl

To fix this, enable SSL on the site everywhere, update wp-config.php (you have to create a docker volume and mount /var/www/html on it, or create your own docker image) to have $_SERVER['HTTPS'] = 'on'; and update the database table so that these 2 options are set correctly:


MariaDB [blog]> select * from wp_options where option_value like 'https://%';
+-----------+-------------+--------------------------+----------+
| option_id | option_name | option_value | autoload |
+-----------+-------------+--------------------------+----------+
| 1 | siteurl | https://blog.mikeski.net | yes |
| 33 | home | https://blog.mikeski.net | yes |
+-----------+-------------+--------------------------+----------+
2 rows in set (0.00 sec)

FFMPEG make an animated gif out of a video

Create the palette first – skip 60 seconds and make it 6 seconds long and 800 px wide with a matching height:

Create the gif, same time parameters

React – new project

create-react-app projectName

cd projectName

npm install

npm start

npm --save install material-ui
npm --save install isomorphic-fetch
npm --save install react-tap-event-plugin
npm --save install react-redux
npm --save install redux-thunk
npm --save install fetch-polyfill

In App.js:

import {MuiThemeProvider} from 'material-ui';
import injectTapEventPlugin from 'react-tap-event-plugin';

constructor(props) {
super(props);
...
}

render(){
return (

...

)

Docs are here: http://www.material-ui.com/#/

Icons are here: https://material.io/icons/

JQuery Stateful Plugin

Creating a JQuery plugin (https://github.com/mikebski/demano) for filtering objects on a page, I needed a way to create a stateful JQuery plugin.

This is done by attaching all of the configuration for the plugin to the actual DOM element we create the plugin on. This allows multiple separate “instances” of the plugin on the page. The only catch is that each must be on a separate DOM entity.

Here’s the example code, here it is on JSFiddle: https://jsfiddle.net/mbaranski/q2rarzfn/ and here is the github plugin: https://github.com/mikebski/demano/:

Note that we proxy the click listener so that “this” in the callback function points to the DOM element that the configuration is attached to.

Here’s a practical example -> https://github.com/mikebski/demano

Java EE Custom Annotation Lookup

Thinking about plugins in a webapp, I wanted a way to have an interface that my code would call when certain things happened.

As a POC, I created an annotation and interface to run after an action completes. First we create the interface:

We will call the afterComplete() method when the action is complete. For simplicity, it just takes a String.

Now, we need an annotation so we can look it up in the container’s context:

This is a simple annotation that allows us to locate each bean that is annotated with it, and get the implementing class. If we create the EJB we want to discover like this, we can easily find it. This is a simple class that prints a log message:

The following code will find all the EJBs that have our @AfterComplete annotation and call their afterComplete(…) method with a simple String:

We can use this method in a Servlet, REST endpoint, or whatever else we want to create callbacks for different methods. There is an example Servlet and 2 beans that have a discover-able annotation at https://github.com/mikebski/ejb-custom-annotations that is a Maven project that can be used for a demonstration.

Check it out and run mvn package to generate a war file, then deploy it and go to:

http://localhost:8080/ejb-custom-annotations-1.0/ to see the output. The output should be:

class net.mikeski.ejb_annotations.AfterCompletePrinterclass net.mikeski.ejb_annotations.AfterCompleteErrPrinter

and your console should have 2 log messages like this:

18:45:51,937 INFO [stdout] (default task-43) After complete printer: After complete called on Proxy for view class: net.mikeski.ejb_annotations.IAfterComplete of EJB: AfterCompletePrinter

18:45:51,939 ERROR [stderr] (default task-43) After complete called on Proxy for view class: net.mikeski.ejb_annotations.IAfterComplete of EJB: AfterCompleteErrPrinter

Angular helper to encode object as www/formdata

Helper to serialize data to www/formdata – needed for turning a javascript object into encoded form data to POST: