Phonegap/Cordova facebook login not working when already connected

匿名 (未验证) 提交于 2019-12-03 08:28:06

问题:

I'm developing and HTML5/CSS/Javascript PhoneGap app that should work as native app for android and iOS and is compiled using the PhoneGap Build service. The app uses Facebook login.

The login is done through the JAVASCRIPT Api. So I have this simple HTML page:

<!DOCTYPE html>  <head> <meta charset="utf-8" /> <meta name="format-detection" content="telephone=no" /> <meta name="msapplication-tap-highlight" content="no" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" /> <link href="style.css" rel="stylesheet" type="text/css" /> <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script> <script src="cordova.js" type="text/javascript"></script> </head>  <body>  <div id="fb-root"></div>  <script type="text/javascript">  $(document).on('click', '#login', function(){    facebookConnectPlugin.login(["email,user_birthday,publish_actions"], function(response) {       if(response.authResponse) {          alert(response.authResponse.userID);       }    });  }); </script>  <img id="login" src="images/login.png" alt="" />  </body>  </html> 

The above code works well when the user hasn't authenticated the app before. So when a user clicks the login button and the user accepts the login, the user id is returned successfully and the session will stick well when the use closes and reopens the app.

The problem lies when the user uninstalls the app and reinstalls again. When they click the login button, the login dialog will open quickly and close again as the user is already authenticated in the app. The user id and access token should return in the response like the regular javascript api of Facebook. Instead the second function which is failure will be returned with the following error:

session was closed and was not closed normally 

I tried to logout the user but I got the following error:there is no session initiated...

I have added the following to the config.xml file:

<gap:plugin name="com.phonegap.plugins.facebookconnect" version="0.9.0">    <param name="APP_ID" value="xxxxxx" />    <param name="APP_NAME" value="App Name" /> </gap:plugin> 

Currently the Facebook app has only Website as platform. Do I need to add Android as platform while I'm using the javascript API for authentication?

Is there any solution for this?

回答1:

Finally solved this! The solution for anyone who encounters this:

1) Generate a keystore for your app. Here is the tutorial to do it:

http://base.techority.com/2013/01/01/how-to-generate-a-key-for-your-android-app-on-a-mac/

2) Generate a hash for the keystore. Here is the doc to do it:

https://developers.facebook.com/docs/android/getting-started#release-key-hash

3) Add Android as a platform in the Facebook app. Enter the hash obtained from the last step into the "Key Hashes" section in the android configuration. Also enter the app package name in the "Google Play Store Package" section. You should do this even if you have not submitted your app to the play store yet. Also switch "Sign Sign On" option to on.

4) After you upload the app to Phonegap Build, click the "Builds" tab.

5) Click the keys drop down menu for the android platform. Click "add new key..."

6) Upload the .keystore file and enter the "Alias" you choose when you created the keystore.

7) Click the "Rebuild" button to assign the keystore to the android app. You will get an error that the key is locked with simple instructions to unlock it.

8) After you unlock the keystore, click "Rebuild" again and your new final Android apk will be rebuilt successfully.

Now you should not run any issues again with the Facebook login :)



回答2:

Problem is with phonegap-facebook-plugin\platforms\android\res\values\facebookconnect.xml

You need to manually add this in facebookconnect.xml

<resources>     <string name="fb_app_id">xxxxxxxxxx</string>     <string name="fb_app_name">fb namespace here</string> </resources> 

I wasted 2 days for this and everywhere was suggesting me to check keyhash

This is documentation :- https://github.com/Wizcorp/phonegap-facebook-plugin/blob/master/platforms/android/README.md



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!