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
prachi aryaprachi arya 

How can I create a lightning button and on-click, it should direct to external app authentication page using OAuth?

Deepali KulshresthaDeepali Kulshrestha
Hi Prachi,

If its an unauthenticated site. Then you can create a custom button and select the 'Content Source' as URL then in the script window past the URL of the site. If it authenticated site, then you have to make successful authentication and then redirect to the URL which you want and also you have to select the 'content source' as 'javascript' while creating a button. 
Check this below example for the one drive authentication using the custom javascript button.

{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")} 

var url = "https://login.live.com/oauth20_authorize.srf?client_id=XXXXXXXXXXXX&scope=onedrive.readwrite&response_type=token&redirect_uri= https://login.salesforce.com/5009000000dssdJ"; 
popup(url); 

function popup(url) { 
var width = 525, 
height = 525, 
screenX = window.screenX, 
screenY = window.screenY, 
outerWidth = window.outerWidth, 
outerHeight = window.outerHeight; 

var left = screenX + Math.max(outerWidth - width, 0) / 2; 
var top = screenY + Math.max(outerHeight - height, 0) / 2; 

var features = [ 
"width=" + width, 
"height=" + height, 
"top=" + top, 
"left=" + left, 
"status=no", 
"resizable=yes", 
"toolbar=no", 
"menubar=no", 
"scrollbars=yes"]; 
var popup = window.open(url, "oauth", features.join(",")); 
if (!popup) { 
alert("failed to pop up auth window"); 


popup.focus(); 



function onAuthCallback() { 
var authInfo = getAuthInfoFromUrl(); 
var token = authInfo["access_token"]; 
var expiry = parseInt(authInfo["expires_in"]); 
setCookie(token, expiry); 
window.opener.onAuthenticated(token, window); 


function getAuthInfoFromUrl() { 
if (window.location.hash) { 
var authResponse = window.location.hash.substring(1); 
var authInfo = JSON.parse( 
'{"' + authResponse.replace(/&/g, '","').replace(/=/g, '":"') + '"}', 
function(key, value) { return key === "" ? value : decodeURIComponent(value); }); 
alert(authInfo); 
return authInfo; 

else { 
alert("failed to receive auth token"); 

}

The above example uses the access token login mechanism to authenticate to 'one drive' application from the custom javascript button in Salesforce.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
prachi aryaprachi arya
Hi Deepali,
Thanks for the reply. But I have to implement it using only lightning components.