■
This article will go over the steps for a successful authorization:
Write custom code to authenticate
With the Salesforce connected app created and configured, we're ready to develop a custom app code needed to authenticate from an external application.
This can be done from various technologies and languages. In this article, we’ll be using an open source .NET CMS & application development framework (DNN) to illustrate the needed steps to authenticate. Accordingly, the code written in our app will be written in C# and ASP.NET.
Like Simpplr, DNN is a very extensible platform that allows developers to create custom extensions. The most popular extension type in DNN is the custom module, which is very similar to Simpplr’s tile feature. We'll leverage a custom module in DNN, and will be omitting some DNN specific details around development that aren't relevant.
Note
Parts of this code should be combined to run more efficiently, and can be refactored with fewer methods. You should customize and refactor your code as needed.
Note
Salesforce has documentation on this process here: OAuth 2.0 Web Server Flow for Web App Integration.
Get the authorization code
The first step is to get an authorization code from Salesforce. To do this, we need to hit a specific URL endpoint passing in a few parameters that are unique to our connected application. Those unique parameters are the client_id, redirect_uri, and response_type.
An example of the full URL:
https://login.salesforce.com/services/oauth2/authorize?client_id=3MVG9ZF4bs_.MKuhGmLLlMI4SoAAORRh.nl862RFi_mIEP7kNoZGOG8QlcET.nqHXXbljpW4jc9GiNbR9ZWWd&redirect_uri=https://clintpatterson.com/Simpplr&response_type=code
- In our custom DNN module, there is a button that, when clicked, will access the unique endpoint. The URL accessed in this click event has been concatenated to make it easier to understand and update.
- When clicked, the code is executed and the browser is redirected to the specified URL, which will prompt the user to allow access to the Salesforce environment on their first time requesting the authorization code.
- After logging in, the browser redirects the user to the url specified in the Redirect URI parameter along with appending the authorization code as a parameter.
concatenated URL
Salesforce allow prompt when requesting authorization code
browser redirect to specified URL
Get the access token
Now that we have the authorization code, we are ready to get the access token. To get the access token:
- We create an HTTPClient so that we can make an HTTP request. In this request, we create a dictionary of FormURLEncoded content that will send our unique information in the request. Notice the client ID & client secret (that we received when we set up our connected app) included in this request, along with the grant_type and authorization code.
- With our request ready, we make an asynchronous request and parse the response as JSON. In the example code, we've updated labels with all of the info that the request returned. From the response we received an access token, but we also got a refresh token and more informative information. We will use the access token to get info from the Simpplr API endpoint.
- We're now successfully authenticated.
HTTP request
access token
Comments
Please sign in to leave a comment.