B4A Library Google Plus SDK Wrapper + Native Google +1Button

Introduction

This is my second contribution to the discovery of the decade (#b4a-rocks) and the fun continues!

Used SocialApi classes:
  • GooglePlusProvider: A non activity object that must be declared in any Sub Process_Globals.
  • GooglePlusOneButton: A CustomView (implements DesignerCustomView) that can be used design-time.
  • SocialApiActivity: An activity object that can be only declared in a Sub Globals and is used to bind a GooglePlusProvider object to an activity through the GooglePlusProvider.SetActivity method.
Getting started
  1. Download the latest socialapi package, extract it anywhere on your hard disk and note the folder name.
  2. Copy the wrapper files (jar + xml) in the socialapi folder into your B4A Libraries folder.
  3. Each sample in the socialapi\googleplus\samples folder has AdditionalRes and AdditionalJar directives that have to be changed. Change the C:\b4a-dev folder to where you have extracted the socialapi archive.

The samples folder contains:
  • Sample1: Quick start sample that can be used as a bare-bone template for your new apps
Step-by-step new app tutorial (based on Sample1)

1. Add following code to your manifest:
B4X:
AddApplicationText(
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version"/>)


2. Add following directives to your project's main file:
B4X:
#AdditionalRes: <your-installation-folder>\socialapi\googleplus\sdk\res, com.google.android.gms
#AdditionalJar: <your-installation-folder>\socialapi\googleplus\sdk\gplussdk.jar


3. Select the SocialApi library from the list of the available libraries in the IDE. Declare a GooglePlusProvider and a SocialApiActivity object in Sub Process_Globals and Sub Globals respectively
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

    Dim GooglePlus As GooglePlusProvider
End Sub

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


4. Drop a CustomView in the designer and set the CustomType property to GooglePlusOneButton. Then, Generate a Click handler.

upload_2014-7-2_19-50-26.png
upload_2014-7-2_19-50-35.png

B4X:
Sub BtnPlusOne_Click
    Log("+1 Clicked!")
End Sub


5. Call the GooglePlusProvider.SetActivity method in your Sub Activity_Resume in order to bind the GooglePlusProvider instance to the current activity passing the name of the event that will be raised every time the GooglePlus session changes. Also, call the BtnPlusOne.SetUrl to set the button's destination url.
B4X:
Sub Activity_Resume
    BtnPlusOne.SetUrl("http://www.b4x.com")
    GooglePlus.SetActivity(ThisActivity.Initialize("googleplus"))
End Sub


6. Create your event handlers
B4X:
Sub GooglePlus_Event (Provider as SocialApiProvider)
    BtnConnect.Enabled = Not(Provider.Connected)
    BtnDisconnect.Enabled = Provider.Connected
End Sub

Sub GooglePlust_Connected (Provider as SocialApiProvider)
    Msgbox(Provider.Me, "!")
End Sub

Sub GooglePlus_Disconnected (Provider as SocialApiProvider)
    Msgbox("Bye bye!", "JustDisconnected!")
End Sub

Sub GooglePlust_Failed (Provider as SocialApiProvider)
    If Msgbox2("Failed to actualize your details."&CRLF&CRLF&"Retry?", "Error", "Yes", "No", "", Null) = DialogResponse.POSITIVE Then
        Provider.Retry
    End If
End Sub


7. Create the login/logout handlers. The login and logout actions are performed asynchronously
B4X:
Sub BtnConnect_Click
    GooglePlus.Login
End Sub

Sub BtnDisconnect_Click
    GooglePlus.Logout
End Sub


8. Do something and evaluate the result
B4X:
Sub BtnGetPeopleVisible_Click
    GooglePlus.PleaseWaitText = "Getting people information"
    Dim Result As GooglePlusResult = GooglePlus.GetPeopleVisible
    Msgbox(Result, "Done")
End Sub

Sub BtnGetPeople_Click
    GooglePlus.PleaseWaitText = "Getting Erel's and Periklis' details"
    Dim Result As GooglePlusResult = GooglePlus.GetPeople(Array As String ("102781867331980802136", "+PeriklisKoutsogiannis"))
    Msgbox(Result, "Done")
End Sub

Done! Now we have to setup our app in our Google+ developer dashboard.

Setup our app in the Google+ developer dashboard (courtesy of androidhive.info)

1. Get your SHA1 fingerprint from B4A IDE

sha-jpg.26015


2. Open Google APIs console and on the left, under APIs & auth section, click on APIs and on the right enable Google+ API service.

google-console-api-google-plus.png


3. Now again on the left, click on Credentials and on the right, click on CREATE NEW CLIENT ID button. It will open a popup to configure a new client id.

google-console-creating-new-client-id.png


4. In the popup, select Installed application as Application type. Under Installed application type section select Android and give your project package name. This package name should be equal to your android project. For the sake of the example the package name is info.androidhive.gpluslogin. Enter your SHA1 fingerprint (from step 1) in Signing certificate fingerprint (SHA1) field and enable Deep Linking and click on Create Client ID button.

google-api-console-creating-new-client-id.png


5. Now you should see a new client created for your android application.

google-api-client-id.png


Done! You are ready to go! :D


Useful links

Please test and post any feedback, questions, comments you may have!
That's all for now folks!~


Version history until SocialApi 2.3

2.3
  • Restructured again into a single package (socialapi) after finding out how to properly use the #AdditionalRes directive to avoid conflict with other b4a libraries
2.2
  • Restructured due to a conflict with AdMod library and GooglePlusProvider has its own package
2.0
  • Now part of the SocialApi unified wrapper library
  • Major code refactoring
  • Renamed: GooglePlus -> GooglePlusProvider
  • Renamed: WrapperActivity -> SocialApiActivity
  • Renamed: WrapperActivity.Initialize -> SocialApiActivity.GetWithEvent
  • Added: Google +1 button custom view
1.2
  • Change: Complete code refactoring. Now using our SocialApiWrapper library that provides common functionality to our SDK wrappers
1.1
  • Added: GooglePlus.GetPeopleVisible
1.0
  • Initial version
 

Attachments

  • sample1-2.jpg
    sample1-2.jpg
    26.3 KB · Views: 701
  • sha.jpg
    sha.jpg
    91.5 KB · Views: 799
  • sample1-1.png
    sample1-1.png
    64.9 KB · Views: 489
  • sample1-3.png
    sample1-3.png
    82.3 KB · Views: 357
Last edited:

Periklis Koutsogiannis

Active Member
Licensed User
Longtime User
Clean your project.

Also if you are using both SDKs you have to have 2 WrapperActivity objects:

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

    Dim FB As Facebook
    Dim GP As GooglePlus
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private BtnConnect As Button
    Private BtnDisconnect As Button

    Dim FBActivity As WrapperActivity
    Dim GPActivity As WrapperActivity
End Sub

Sub Activity_Resume

    FB.SetActivity(FBActivity.Initialize("facebook_status_changed"))
    GP.SetActivity(GPActivity.Initialize("googleplus_status_changed"))
End Sub

Sub facebook_status_changed
End Sub

Sub googleplus_status_changed
End Sub
 

Douglas Farias

Expert
Licensed User
Longtime User
finally *-*
aaaah_FIM_DE_SEMANA.gif


my sha1 is incorrect ths is changed when i close the b4a *-*

thx man now is working fine *-*
 

Periklis Koutsogiannis

Active Member
Licensed User
Longtime User
Nice!

Please check my previous post about the 2 WrapperActivity objects. Your code as it was posted will not work correctly.
 

Periklis Koutsogiannis

Active Member
Licensed User
Longtime User
Update: 2.0

  • Now part of the SocialApi unified wrapper library
  • Major code refactoring
  • Renamed: GooglePlus -> GooglePlusProvider
  • Renamed: WrapperActivity -> SocialApiActivity
  • Renamed: WrapperActivity.Initialize -> SocialApiActivity.GetWithEvent
  • Added: Google +1 button custom view

How to migrate from previous versions:
  1. Delete GooglePlus.jar SimpleApiWrapper.jar and GooglePlus.xml SimpleApiWrapper.xml from your B4A Libraries folder
  2. Download the latest gplussdk.zip, delete the old folder and extract the contents of the new one
  3. Select the SocialApi library from the list of the available libraries in the IDE
  4. Rename all mentioned classes in the change log above
Done!
 
Last edited:

GMan

Well-Known Member
Licensed User
Longtime User
Well done, so next step is the FB LikeButton ? :rolleyes:
 

GMan

Well-Known Member
Licensed User
Longtime User
Ah, OK - and Posting something or sending Picture ?
 

GMan

Well-Known Member
Licensed User
Longtime User
best both ;-)
 

GMan

Well-Known Member
Licensed User
Longtime User
Thx...read it aber deleted it (as it seems) in my cpu :confused:
 

Periklis Koutsogiannis

Active Member
Licensed User
Longtime User
Update: 2.2

  • Restructured due to a conflict with AdMod library and GooglePlusProvider has its own package. Please read the Getting started for updated installation instructions!
 
Last edited:

Periklis Koutsogiannis

Active Member
Licensed User
Longtime User

JeanLC

Member
Licensed User
Longtime User
Hello,
How can I retrieve the google token?

Using this library/wrapper... After Google one button sign-in, and authenticated by the App and Google.
How can I retrieve the token?
The token the app can send to a backend server to check user authentication with google.
In the API it's GoogleAuthUtil.getToken
then you check from backedn server with https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=<how to get this token>
and you confirm the info from google with a response like this:
{
"iss": "https://accounts.google.com",
"sub": "110169484474386276334",
"azp": "1008719970978-hb24n2dstb40o45d4feuo2ukqmcc6381.apps.googleusercontent.com",
"email": "[email protected]",
"at_hash": "X_B3Z3Fi4udZ2mf75RWo3w",
"email_verified": "true",
"aud": "1008719970978-hb24n2dstb40o45d4feuo2ukqmcc6381.apps.googleusercontent.com",
"iat": "1433978353",
"exp": "1433981953"
}
so the server knows that's the real user without additional sign-in.

https://developers.google.com/identity/sign-in/android/backend-auth

Thanks
 
Top