function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Anupam RastogiAnupam Rastogi 

Programmatically extracting Authorization Code for the Web Server OAuth Authentication Flow

Hi Everyone,

I am new to Salesforce coding and learning to integrate with Salesforce. I am facing issue as mentioned below.

Scenario - I am integrating two Salesforce instance using APEX Callout and REST API. In the source SFDC instance I have created a trigger on the Account Object that calls a class with @future annotation. This class sends out a HTTP Request to the other SFDC instance using Web Server Authentication Flow.

Issue Description - Per the defined steps in 'Getting Started with the REST API' guide, I create a Connected App record in the Destination SFDC instance. But when the APEX code is requesting the Salesforce Authorization Endpoint with the relevant parameters (response_type, client_id, redirect_uri), I am not able to retrieve the Authorization Code (Code) that comes along with the Callback URL.
Though, I am successfully redirected to the callback URL (appended with the code) when I am using a browser. See below the request and the redirected response I get when I do it in a browser - 
HTTP Request:[client_id taken from the Connected App record]&redirect_uri=https://localhost:8443/RestTest/oauth/_callback&immediate=true
HTTP Response:

Problem Statements - 
1. On the first place, is it possible to programmatically extract the code from the HTTP response?

2. If the answer to the above question is 'Yes', then please suggest, how? What are the relevant methods to extract this information.

Did you find an  answer to it ?
Zeeshan JanZeeshan Jan
Any updates on this?
i'm facing the same problem , any updates for this please?
faisal khan 39faisal khan 39
Hope this answer may help someone although it is late but it is "Better to be late than never".

It is not possible to get the code without login into the browser.Therefore while using web server flow we need to login into the browser then we will be redirected to the callback URL.

Programatically we will not be able to get the code without browser login !!

Hope this answer your question!!