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
Emtecinc developer049388157163885804Emtecinc developer049388157163885804 

Salesforce Authentication using Oauth2 for Hybrid mobile app

Hello,

I am trying to get the access token from SF for my Mobile app.
I am not using Salesforce SDK.
I am using Phonegap as I intend to create a hybrid app.

I have written the following code :-
var loginURL = "https://login.salesforce.com/";
    var consumerKey = '3MVG9Y6d_Btp4xp4FClT2rkPVFKe3ochPsDJ.131PRuoUqzEHGysmx78HcFwCMqvWuCTX3kkcrtiULLTBz.q4';
    //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
    alert(authUrl);
    var authWindow = window.open(authUrl, "_blank",'toolbar=yes ,location=no');


This is an InAppBrowser implementation to get the access code in the URL .

However, I am unable to assign events to this child window instance.
$(authWindow).on('loadstop', function(e) { });  // This event binding is not working for me.

Please help me in order to get the access code in my parent app.

Thanks
Ashish_SFDCAshish_SFDC
Hi , 


So https://login.salesforce.com/services/oauth2/success is a stub URL for callbacks - and that is the screen you are seeing there, and the access_token variable is your session ID for the user. So the OAuth flow is working correctly.

In the code, you might check to see if:

oauthResponse.access_token
Is getting set correctly. If so it looks like:

sfw.login( setupHomeView );
Is what should tell it to move from that page to the next UI page.

You might also check out the Force.com Mobile SDK: http://developer.force.com/MobileSDK

It also includes PhoneGap and has a great OAuth wrapper built in.

http://stackoverflow.com/questions/11191311/oauth2-issue-in-salesforce-phonegap-iphone-application-remote-access-applicatio

See further information, 

Refreshing Expired Mobile Salesforce Sessions
http://www.cloudspokes.com/challenges/1546

Building PhoneGap Mobile Applications
http://wiki.developerforce.com/page/Building_PhoneGap_Mobile_Applications_Powered_by_Database.com

SalesforceSDK / src / com / salesforce / androidsdk / phonegap / SalesforceOAuthPlugin.java
https://github.com/jschultz76/SalesforceSDK/blob/master/src/com/salesforce/androidsdk/phonegap/SalesforceOAuthPlugin.java


Regards,
Ashish

Emtecinc developer049388157163885804Emtecinc developer049388157163885804
Hi,

Just to be clear , i want to achieve this without using Salesforce SDK.

Somehow I need to pass the access token from InAppBrowser window into Parent App from where I opened the window.

Thanks
Ashish_SFDCAshish_SFDC
Hi, 

See the "sfw.login( setupHomeView );" in the link below, which uses the wrapper class. 

http://www.adobe.com/devnet/phonegap/articles/phonegap-apps-powered-by-developercom.html

See the demo Develop Mobile Web and Hybrid Apps on Force.com

http://www.youtube.com/watch?v=auMyviKiGH0

oAuth 2.0 for Salesforce.com

http://www.modelmetrics.com/technology-viewpoint/oauth-2-0-for-salesforce-com/

Regards,
Ashish