SocialApi SDK Wrappers - B4A Single-Sign-On with multiple providers

Periklis Koutsogiannis

Active Member
Licensed User
You must use 1 SocialApiActivity per Activity. And since you have more than 1 providers, you have to use only 1 set of events that will be called by all providers.
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Dim ThisActivity As SocialApiActivity
End Sub

Sub Activity_Resume
    'Bind all providers to the same SocialApiActivity at once
    ThisActivity.InitializeAndBind(Array As SocialApiProvider (Facebook, GooglePlus), "Provider")
End Sub

The set of the events that will be raised by all providers:
B4X:
Sub Provider_Event (Provider As SocialApiProvider)
    BtnConnectFacebook.Enabled = Not(Facebook.Connected)
    BtnDisconnectFacebook.Enabled = Facebook.Connected

    BtnConnectGooglePlus.Enabled = Not(GooglePlus.Connected)
    BtnDisconnectGooglePlus.Enabled = GooglePlus.Connected
End Sub

Sub Provider_Connected (Provider As SocialApiProvider)
End Sub

Sub Provider_Disconnected (Provider As SocialApiProvider)
End Sub

Sub Provider_Failed (Provider As SocialApiProvider)
    If Msgbox2("Provider '" & Provider.Type & "' failed to actualize your details."&CRLF&CRLF&"Retry?", Provider.Type & ": Error", "Yes", "No", "", Null) = DialogResponse.POSITIVE Then
        Provider.Retry
    End If
End Sub
Check the socialapi\multiple\sample2 project
 
Last edited:

Periklis Koutsogiannis

Active Member
Licensed User
Your Sub Provider_Connected should be like this:
B4X:
Sub Provider_Connected (Provider As SocialApiProvider)
    If Provider Is FacebookProvider Then
        Dim Facebook as FacebookProvider = Provider
        If Not(Facebook.HasPermission(Facebook.Constants.Permissions.PUBLISH_ACTIONS)) Then
            Facebook.RequestPublishPermissions
        End If
    End If
    idsocial = Provider.Me.Get("id")
End Sub

Do not forget that the events are raised by all providers, so if you want to do provider specific tasks, you should always check if the Provider is <provider_type> like this:
B4X:
If Provider Is FacebookProvider Then
    Dim Facebook as FacebookProvider = Provider
    Facebook.<something>
End If

If Provider Is GoogleProvider Then
    Dim GooglePlus as GooglePlusProvider = Provider
    GooglePlus.<something>
End If
 
Last edited:

Douglas Farias

Expert
Licensed User
if i need use facebook.post on activity 2

i can user this?
B4X:
If Provider Is FacebookProvider Then
    Dim Facebook as FacebookProvider = Provider
    Facebook.<something>
End If

If Provider Is GoogleProvider Then
    Dim GooglePlus as GooglePlusProvider = Provider
    GooglePlus.<something>
End If
because Dim Facebook as FacebookProvider is not on common
 

Periklis Koutsogiannis

Active Member
Licensed User
It is the same.

Dim Facebook as FacebookProvider = Provider will get a handle to Common.Facebook object. Facebook will equal Common.Facebook
 

Douglas Farias

Expert
Licensed User
B4X:
If Provider Is FacebookProvider Then
    Common.Facebook = Provider
    Facebook.<something>
End If

If Provider Is GoogleProvider Then
    Common.Google= Provider
    GooglePlus.<something>
End If
its correct can i use this?
 

Periklis Koutsogiannis

Active Member
Licensed User
No it is not.

you either do:
B4X:
If Provider Is FacebookProvider Then
    Dim Facebook as FacebookProvider = Provider
    Facebook.<something>
End If
or:
B4X:
If Provider Is FacebookProvider Then
    Common.Facebook.<something>
End If
 

Douglas Farias

Expert
Licensed User
ok now i make this

B4X:
Sub imgface_Click
        Common.Facebook.Login(Array As String (Common.Facebook.Constants.Permissions.Email))
End Sub

Sub imggoogle_Click
        Common.GooglePlus.Login
End Sub


Sub Provider_Event (Provider As SocialApiProvider)
'nada aqui
End Sub

Sub Provider_Connected (Provider As SocialApiProvider)
    If Provider Is FacebookProvider Then
        Dim Facebook As FacebookProvider = Provider
        If Not(Facebook.HasPermission(Facebook.Constants.Permissions.PUBLISH_ACTIONS)) Then
            Facebook.RequestPublishPermissions
        End If
    End If
   
    If Provider Is FacebookProvider Then
    Msgbox(Provider.Me,"face")
    End If
   
    If Provider Is GooglePlusProvider Then
    Msgbox(Provider.Me,"google")
    End If
   
   
    'idsocial = Provider.Me.Get("id")
End Sub

Sub Provider_Disconnected (Provider As SocialApiProvider)
End Sub

Sub Provider_Failed (Provider As SocialApiProvider)
    If Msgbox2("Provider '" & Provider.Type & "' failed to actualize your details."&CRLF&CRLF&"Retry?", Provider.Type & ": Error", "Yes", "No", "", Null) = DialogResponse.POSITIVE Then
        Provider.Retry
    End If
End Sub

ok i can login with facebook and show my profile i can click at button1 to logout of facebook and is ok too

with the google i can login , show my profile
but when i press a button2 to logout google
this make a logout but show my profile again.


B4X:
Sub Button1_Click
    Common.Facebook.Logout
End Sub
Sub Button2_Click
        Common.GooglePlus.Logout
End Sub
 

Douglas Farias

Expert
Licensed User
B4X:
Sub Provider_Connected (Provider As SocialApiProvider)
    If Provider Is FacebookProvider Then
        Dim Facebook As FacebookProvider = Provider
        If Not(Facebook.HasPermission(Facebook.Constants.Permissions.PUBLISH_ACTIONS)) Then
            Facebook.RequestPublishPermissions
        End If
    End If
   
    If Provider Is FacebookProvider Then
    Msgbox( Common.Facebook.Me,"face")
    End If
   
    If Provider Is GooglePlusProvider Then
    Msgbox(Common.GooglePlus.Me,"google")
    End If
           
   
    'idsocial = Provider.Me.Get("id")
End Sub
only a msgbox with

Msgbox( Common.Facebook.Me,"face")

or

Msgbox(Common.GooglePlus.Me,"google")

when i log on with google show me this
Msgbox(Common.GooglePlus.Me,"google")

when i click to logout with google
show again this
Msgbox(Common.GooglePlus.Me,"google")
 

Douglas Farias

Expert
Licensed User
http://www.likeapps.net/click.apk



B4X:
#Region  Project Attributes
    #ApplicationLabel: Click Fight
    #VersionCode: 1
    #VersionName: 1.0
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
    #AdditionalRes: ..\AppConfig
    #AdditionalRes: C:\b4a-dev\socialapi\facebook\sdk\res
    #AdditionalJar: C:\b4a-dev\socialapi\facebook\sdk\facebooksdk.jar
    #AdditionalRes: C:\b4a-dev\socialapi\googleplus\sdk\res, com.google.android.gms
    #AdditionalJar: C:\b4a-dev\socialapi\googleplus\sdk\gplussdk.jar
   
#End Region

#Region  Activity Attributes
    #FullScreen: True
    #IncludeTitle: False
#End Region

Sub Process_Globals
End Sub

Sub Globals
        Private splash As SplashFade
        Private imgface As ImageView
        Private imggoogle As ImageView
        Private imgvallow As ImageView
        Private lgoogle As Label
        Private lface As Label
        Private logoimg As ImageView
            Private ThisActivity As SocialApiActivity
        Private recebeconfereusuario = "recebeconfereusuario", versaodoapp = "versaodoapp" As String
        Private idsocial As String
        Private Button1 As Button
        Private versionString As String
        Private pm As PackageManager
        Private p As PhoneIntents
    Private Button2 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
        Activity.LoadLayout("main")
        splash.Initialize(Activity, Me,"splash.png", Gravity.FILL, 3000,100,"Left",False)
       
        Try
        versionString = pm.GetVersionName("achou.ganhou.vallow")
        verifica_appversao_app
        Catch
        End Try
End Sub

Sub Activity_Resume
      ThisActivity.InitializeAndBind(Array As SocialApiProvider (Common.Facebook, Common.GooglePlus), "Provider")

End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub imgface_Click
        Common.Facebook.Login(Array As String (Common.Facebook.Constants.Permissions.Email))
End Sub

Sub imggoogle_Click
        Common.GooglePlus.Login
End Sub


Sub Provider_Event (Provider As SocialApiProvider)
'nada aqui
End Sub

Sub Provider_Connected (Provider As SocialApiProvider)
    If Provider Is FacebookProvider Then
        Dim Facebook As FacebookProvider = Provider
        If Not(Facebook.HasPermission(Facebook.Constants.Permissions.PUBLISH_ACTIONS)) Then
            Facebook.RequestPublishPermissions
        End If
    End If
   
    If Provider Is FacebookProvider Then
    Msgbox( Common.Facebook.Me,"face")
    End If
   
    If Provider Is GooglePlusProvider Then
    Msgbox(Common.GooglePlus.Me,"google")
    End If
           
   
    'idsocial = Provider.Me.Get("id")
End Sub

Sub Provider_Disconnected (Provider As SocialApiProvider)
End Sub

Sub Provider_Failed (Provider As SocialApiProvider)
    If Msgbox2("Provider '" & Provider.Type & "' failed to actualize your details."&CRLF&CRLF&"Retry?", Provider.Type & ": Error", "Yes", "No", "", Null) = DialogResponse.POSITIVE Then
        Provider.Retry
    End If
End Sub







    Sub ExecuteRemoteQuery(query As String, JobName As String)
        Dim jobs As HttpJob
        jobs.Initialize(JobName, Me)
        jobs.PostString("http://www.clickfight.com.br/queryclick.php", query)
    End Sub


    Sub confereusuario
        ExecuteRemoteQuery("SELECT * FROM usuarios WHERE UserID = '"&idsocial&"'" , recebeconfereusuario)
    End Sub
   
    Sub verifica_appversao_app
        ExecuteRemoteQuery("SELECT * FROM versao WHERE Versao > '"&versionString&"'"  , versaodoapp)
    End Sub
   
   
    Sub JobDone (Job As HttpJob)
    If Job.Success Then
    Dim respostadoservidor As String
    respostadoservidor = Job.GetString
   
    Select Job.JobName
   
    Case "versaodoapp"
        If respostadoservidor = "[]" Then
        Log("Verificou a versão na db")
        Else
        Msgbox("A sua versão do aplicativo Click Fight não esta atualizada! para continuar você deve atualizar.", "Achou Ganhou")
          StartActivity(p.OpenBrowser("https://play.google.com/store/apps/details?id=click.fight.vallow"))
        ExitApplication
        End If
   
   
   
   
   
   
   
    Case "recebeconfereusuario"
    Log("recebeu resultado do banco")
   

   
   
    End Select       
    Else
    'Mostra mensagem do erro de conexão
    End If
    Job.Release
End Sub
   

Sub Button1_Click
    Common.Facebook.Logout
End Sub
Sub Button2_Click
        Common.GooglePlus.Logout
End Sub



my common

B4X:
Sub Process_Globals
    Public Facebook As FacebookProvider
    Public GooglePlus As GooglePlusProvider
End Sub
 

Douglas Farias

Expert
Licensed User
thx works fine
another simple question
its possible make a social activity fullscrenn?

for exemple when i click at login with face or google and my app is fullscrenn with no title the face or google activity come with no fullscrenn
 
Top