Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Facebook login on android



  • Hei there :-D
    Can anyone tell me how to implement a facebook login on android?
    Cause since there is no QtWebkit available for android I don't see a solution for this.





  • Hi,
    if using Qt4 is also an option for you, then you can have a look at the "Facebook QML plugin":http://v-play.net/doc/plugins1-facebook/ from V-Play.

    Using it is pretty simple and works for iOS & Android, like in this example:

    @ import VPlay 1.0
    import QtQuick 1.1
    import VPlay.plugins.facebook 1.0

    GameWindow {

    Facebook {

     id: facebook
     appId: "437390992991535" // V-Play Test App
    
     readPermissions: ["email", "read_friendlists"]
     publishPermissions: ["publish_actions"]
    

    }
    }
    @

    You can then post highscores, open a friend picker, start get and post graph requests, post to the user timeline and more.. For example opening a native Facebook dialog where the user can post a custom message to his timeline (aka wall) works like this:
    @

    facebook.openWallDialog( {
    "link" : "www.v-play.net",
    "description" : "This is the description",
    "name" : "header of the message",
    "caption" : "message caption"
    } )
    @

    The full documentation and sample code for using the Facebook item is available "here":http://v-play.net/doc/plugins1-facebook/.

    If you are interested in the Qt5 plugin for Facebook, you can add your email here and receive a short reminder as soon as we have published the standalone Qt5 plugins:
    http://plugins.v-play.net/

    Cheers, Chris (Co-Founder of V-Play)



  • Im not using QML in my app, and i'm not willing to add QML code, but thanks for the offer :D.
    Anyway i now found a way my self. With the help of QtAndroidExtras I wrote some Java code witch should start a new android activity. This then uses androids standard webkit, to open the facebook login page for apps.
    Well, so far, so good. There are still some problems with the java class Intent and the AndroidManifest.xml, hopefully i'll solve them.

    D*** if I saw this engine before, it would have made my developments much easier -.- .



  • Is it possible to use Android facebook SDK inside Qt applications?



  • Well it could be possible. But since facebook SDK is made for the use with Eclipse, you had to merge all the SDK files into your Qt-Project by hand. So I think its a bad idea to use it.



  • Have you done something that works yet?



  • Nop. Not jet. Well since I couldn't solve the problem I worked on something different. But a few days ago I started working on it again. I want to crate a second Android Activity so I can access the regular Android GUI to run WebView, with witch I can show the facebook login page to my app. Well my problem at the moment is, to get another Activity running, besides the QtActivity with witch the qt surface runs. I asked that already in another thread: http://qt-project.org/forums/viewthread/37854/

    But i still couldn't get it working.



  • I'll trying this way too and trying to understand how QtActivity and Intend works.
    But I have one theorical question : if we can subclass the main QtActivity that is used by the application, the only thing that we need is to swap the setContentView.
    Maybe this could resolve our issue without having to run multiple activity?



  • from https://blog.qt.digia.com/blog/2013/07/23/anatomy-of-a-qt-5-for-android-application/
    I can follow my idea as events are dispatched on subclasses.

    And after some tests, It seems that I have a start of solution: I could open a webview into my own View.

    My manifest :
    @ <application android:hardwareAccelerated="true" android:name="org.qtproject.qt5.android.bindings.QtApplication" android:label="@string/app_name" android:icon="@drawable/icon">
    <activity android:screenOrientation="portrait" android:name="com.appli.test.TFacebook" android:configChanges="orientation|uiMode|screenLayout|screenSize|smallestScreenSize|locale|fontScale|keyboard|keyboardHidden|navigation" android:label="@string/app_name">
    <intent-filter>
    <action android:name="android.intent.action.MAIN"/>
    <category android:name="android.intent.category.LAUNCHER"/>
    </intent-filter>

    ...
    <uses-permission android:name="android.permission.INTERNET"/>
    @

    My Layout.xml (in res/layout folder)
    @<?xml version="1.0" encoding="utf-8"?>
    <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Webiew xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_x="150px"
    android:layout_y="150px"
    android:width="100px"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />
    </AbsoluteLayout>
    @
    My .java code (in src/com/appli/test)
    @
    public class TFacebook extends QtActivity
    {
    private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
    view.loadUrl(url);
    return true;
    }
    }
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);

        Log.e(QtApplication.QtTAG, "Setting Webview");
        setContentView(R.layout.activity);
        WebView myWebView = (WebView) findViewById(R.id.webview);
        myWebView.setWebViewClient(new MyWebViewClient());
        myWebView.loadUrl("http://www.google.com");
    

    }
    }
    @

    Note : shouldOverrideUrlLoading avoid to open an external browser.
    The next step will be to come back to the previous layout of the main application.

    I hope this way lead to something.



  • As the behaviour is strange and seems to doesn't correspond to the design pattern of Android, I stopped this way.

    So, I came back to your solution and I could run multiple QtActivity without issue.
    Just declare a simple activity for class that are not the main class (without <intent-filter>) in the application tag.
    in addition to the previous code, my TMain.java is :

    @package com.appli.test;

    import org.qtproject.qt5.android.bindings.QtApplication;
    import org.qtproject.qt5.android.bindings.QtActivity;
    import android.os.Bundle;
    import android.content.Intent;
    import android.util.Log;

    public class TMain extends QtActivity
    {
    public static void startFacebook()
    {// Called from JNI
    getInstance().startFacebookObject();
    }

    public static TMain g_instance;
    public TMain()
    {
    }
    public static TMain getInstance()
    {
        return g_instance;
    }
    public void startFacebookObject()
    {
        try
        {
            Intent s = new Intent(TMain.this, TFacebook.class);
            this.startActivity(s);
        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        try
        {
            Log.e(QtApplication.QtTAG, "Creating FKMain");
            super.onCreate(savedInstanceState);
            g_instance = this;
        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    

    }
    @

    Do you know why using finish() in TFacebook closes the application and don't resume to the previous activity?



  • Well well.
    So at least my code is working to.
    But a bit embarrassing is that it could have worked weeks ago if I just didn't forget to include the package to my app into the code. That's the reason Intent couldn't find my class xD.

    Now to your code. I think I don't understand it fully. Did you replace the main QtActivity with your TMain activity, and then use a second implementation of it to start your Tfacebook Activity ?!?
    If so don't do that, its bad xD. Also it was bedder if you implemented everything in your Tfacebook Activity:

    [code]
    package com.appli.test;

    import org.qtproject.qt5.android.QtNative;
    import android.os.Bundle;
    import android.content.Intent;
    import android.app.Activity;
    import android.util.Log;

    public class TFacebook extends Activity
    {
    private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
    view.loadUrl(url);
    return true;
    }
    }
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);

        Log.e(QtApplication.QtTAG, "Setting Webview");
        setContentView(R.layout.activity);
        WebView myWebView = (WebView) findViewById(R.id.webview);
        myWebView.setWebViewClient(new MyWebViewClient());
        myWebView.loadUrl("http://www.google.com");
    }
    

    public static void login() {
    try {
    Activity mother = QtNative.activity();
    Intent s = new Intent(mother, TFacebook.class);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
    [/code]

    Now you only have to call:
    [code]
    QAndroidObject::callStaticMethod("com.appli.test.TFacebook", "login");
    [/code]

    This also shouldn't close the whole app if you call finish().

    (No guarantee that its completely working. I didn't test this code here.)

    With best regards, Schabi. xD



  • Haha, error reporting is "great" when it comes from android ^^

    I'm using the QML interface (I know you don't). So, I need to keep the activity from Qt, it's why I need to extends the QtActivity (and I will not have only facebook in native side)
    In addition, examples from digia about hangman (for paiment) and their notifications example extends the QtActivity too.
    So I don't think that it's bad ;)

    For the finish(), it was just the " xmlns:android" in the layout that is an error. I detected it by implementing a new application from Eclispe : it could give me error on UI where Qt don't.
    So, I could have something working too ;). I can login and use the Graph API to get what I need.

    The main issue for now is to keep the session open (access token is only for one hour). Do you know what we need to do? (like storing cookies in the app maybe)



  • Seams like we need to request a long live access token witch leasts for 60 days:
    https://developers.facebook.com/docs/facebook-login/access-tokens/#extending



  • I finally got it working !!!
    I was able to merge the Facebook SDK into my Android app, to get 60 day longterm accesstoken.
    If someone is interested please answer me, or write me a mail. Then I'll try to prepare the code so everyone can have it :)

    but despite its working, its not very nice.

    chears schabi



  • Nice work schabi, you can add a link to your code here in the forums if you like?

    Since a couple of days, we now also have a Facebook Qt 5 plugin ready for Android & iOS. You can have a look at the sample on github and the tutorials here:



  • the_scrabi, I would really appreciate a working example to study. My desired result is to add Google Login to Qt Android app and the problem (missing webview) is fairly similar. Also could help me to resolve the Google maps integration issues.

    Another good exmaple to study is probably "Qt hangman ":http://blog.qt.digia.com/blog/2013/12/12/implementing-in-app-purchase-on-android/ since it implements android in-app purchasing an that can't be too different compared to what we need to achieve.



  • All right I'll try to get it ready the next days. But I need to apologise, it still has some problems, and I do this in my spare time. So it will take me a few days to get it working correctly.

    Also this is NOT using a Web login any more !!! Since you could only get short term access token from Web login I went over to the regular facebook sdk for android.

    But if you want to do Web Login here is something that might help you:

    Android Activity Doc:
    http://developer.android.com/reference/android/app/Activity.html

    Intent Doc (you need intent to start new activitys):
    http://developer.android.com/reference/android/content/Intent.html

    QtAndroidExtras (you need it to call/run java code):
    http://qt-project.org/doc/qt-5/qtandroidextras-index.html

    Here a link to an older example of my facebook login thing. It still uses Web login:
    https://github.com/theScrabi/FbLoginQtAndroid.git

    If something is not clear, or you still have questions feel free to mail me :)

    greetings schabi



  • All right i said i would need a few days. I guess I overestimated my self a bit. It now took me about two full months to get it working that prober to give it to other people. But now I also did a small documentation.
    I hope it will help somebody. And if you still have questions or any wishes feel free to mail me.

    Github repository: https://github.com/theScrabi/TizFbExample

    Chears schabi



  • @the_scrabi

    I also stuck to use Fb login in my QML.
    Would you like to share me how to do yourself so will be able to do myself too.

    Yeah ..
    This example is working as per requirement thanks for this.. :)


  • Banned

    This post is deleted!

  • Lifetime Qt Champion

    This post is deleted!

  • Banned

    This post is deleted!

Log in to reply