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
MohanrajMohanraj 

PhoneGap application callback/redirect url issue

Hello, I'm using cordova version 2.9 and forcetk.js.

 

After outh screen i want to redirect to the index.html file of my phonegap app.

I tried using file:/// link but no use.

 

can any one help me in this. How to configure the callback url in local hybrid app.

 

Thanks in advance!!

niki4810niki4810
Hi Mohanraj,

I am assuming that you are launching the SalesForceLogin page via Phonegap's InAppBrowser and once you authenticate and allow permission you want to get back to the home page. If this is your use case, here is a code that you could us for reference

You will never leave your index.html page for authentication but you would simply open a browser that loads the login page.

//on Dom Ready
$(function(){
//Listen for device ready
document.addEventListener("deviceready",onDeviceReady);
//on Device Ready
function OnDeviceReady(){
var loginURL = "https://login.salesforce.com/";
var consumerKey = '<YOUR-KEY>';
//this could be anything, once SF authentication is successful, SF will redirect your to this URL and
//your inAPPBrowser will load this
var callbackURL = 'https://login.salesforce.com/services/oauth2/success';
var client = new forcetk.Client(consumerKey, loginURL);
var authUrl = loginURL + 'services/oauth2/authorize?' + '&response_type=token&client_id=' + encodeURIComponent(consumerKey) + '&redirect_uri=' + encodeURIComponent(callbackURL);

//load the SF login page in an InAppBrowser
var authWindow = window.open(data.authUrl, "_blank", 'location=no,toolbar=no,clearsessioncache=yes');

//register a loadstop event hander on the InAppBrowser, this gets called when ever your
//inAppBrowser changes url
authWindow.addEventListener('loadstop', function(e) {
var loc = e.url;
//when url is changed check if the url contains your specific callbackURL
if (loc.search(data.callbackURL) >= 0) {
//at this point close your inapp browser
//you will land on the index page within your application.
authWindow.close();
//your code after successful authentication
}
});
}
});

Hope this helps

-
Nikhil
MohanrajMohanraj
Thanks!! let me check that..
Also may i know, how to authendicate in username-password flow without asking for username and password.
Emtecinc developer049388157163885804Emtecinc developer049388157163885804
Hi Mohanraj,

I am trying to do the same thing as explained above.
I am able to authenticate using InAppBrowser. However I am not able to bind the loadstart and loadstop event of InappBrowser to the child window.
I want to some how get the access token from URL of child window into the parent app.
Can u pls help me
Leandro Francisco SilveiraLeandro Francisco Silveira
Hi Emtecinc,

Maybe you already know the answer because it been about four months ago, but anyway i will anwser for others ones that have the same problem.
To solve your problem, try to add the InAppBrowser plugin to your project.
I was having this same issue, I added the plugin and then i could bind the event.

Hope this will help!
AmbilyAmbily
Hi Mohanraj,

Were you able to solve this issue?

Thanks,
Ambily
Sanjay KrishnanSanjay Krishnan
HI Nikhil, Ambily, :) New comer to the Inappbrowser world. I have a similar problem to solve - calling my company SSO login page via inappbrowser, pass in the credentials, upon succesful authentication , capture the token. I shall try Nikhil's code tommorrow and see whether it works for me. Meanwhile please can you help with some code snippet, as to how to capture cookies/sessions after succesful authentication via inappbrowser.

Your help here will be very much appreciated and congrats for doing a wonderful job here.

Cheers
Samuel.