Twitter SDK Wrapper

Periklis Koutsogiannis

Active Member
Licensed User
Introduction

Used SocialApi classes:
  • TwitterProvider: A non activity object that must be declared in any Sub Process_Globals.
  • SocialApiActivity: An activity object that can be only declared in a Sub Globals and is used to bind a TwitterProvider object to an activity through the TwitterProvider.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\twitter\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 archive 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(
      <activity
        android:name="com.datasteam.twitter.android.TwitterActivity"
        android:theme="@android:style/Theme.Translucent.NoTitleBar"/>)

2. Add following directives to your project's main file:
B4X:
#AdditionalRes: <your-installation-folder>\socialapi\twitter\sdk\res
#AdditionalJar: <your-installation-folder>\socialapi\twitter\sdk\twittersdk.jar

3. Select the SocialApi library from the list of the available libraries in the IDE. Declare a TwitterProvider 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 Twitter As TwitterProvider
    Twitter.Initialize("<your-consumer-key>", "<your_consumer_secret> ")

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. Call the TwitterProvider.SetActivity method in your Sub Activity_Resume in order to bind the TwitterProvider instance to the current activity passing the name of the event that will be raised every time the TwitterProvider session changes.
B4X:
Sub Activity_Resume
    Twitter.SetActivity(ThisActivity.Initialize("twitter"))
End Sub

5. Create your event with the name that you have previously set
B4X:
Sub Twitter_Event (Provider as SocialApiProvider)
    BtnConnect.Enabled = Not(Provider.Connected)
    BtnDisconnect.Enabled = Provider.Connected
End Sub

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

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

Sub Twitter_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

6. Create the login/logout handlers. The login and logout actions are performed asynchronously
B4X:
Sub BtnConnect_Click
    Twitter.Login(true)
End Sub

Sub BtnDisconnect_Click
    Twitter.Logout
End Sub

8. Do something and evaluate the result
B4X:
    Dim TweetDialog As InputDialog

    If TweetDialog.Show("", "Your tweet", "Tweet!", "Cancel", "", Null) = DialogResponse.POSITIVE Then
        If TweetDialog.Input <> "" Then

            Dim Result As TwitterResult

            Result = Twitter.Tweet(TweetDialog.Input)
            If Result.Success Then
                Msgbox(Result, "Tweet!")
            End If
        End If
    End If
Done! Now we have to setup our app in our Twitter developer dashboard.

Setup our app in Twitter developer dashboard

Follow the steps described here:
http://azure.microsoft.com/en-us/do...vices-how-to-register-twitter-authentication/

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 TwitterProvider has its own package
1.0
  • Initial version
 

Attachments

Last edited:

GMan

Well-Known Member
Licensed User
+1
 

Periklis Koutsogiannis

Active Member
Licensed User
Update: 2.2

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

Periklis Koutsogiannis

Active Member
Licensed User

brelto85

Active Member
Licensed User
i've a strange behavior
after call login() method, appear properly the login page
i insert my credentials and are accepted because in the top-right position appears my icon twitter's, but not returns to my app
 

brelto85

Active Member
Licensed User
same behavior with the last library (2.51)
In the login page, the credentials are accepted (the picture image of my profile is visible in top-right of page) but the login page does not closed automatically
If i close manually, the provider_event fired but the Twitter.Connected property is "false"
 
Last edited:

Periklis Koutsogiannis

Active Member
Licensed User
Last edited:

brelto85

Active Member
Licensed User
your sample1 project works fine with your credentials
if i use my credentials in your project, i've the same behavior of my app

i've this application settings in developer twitter console:

Access level: Read, write, and direct messages
API key: .....
Callback URL: http://legaleoncelli.altervista.org/
Sign in with Twitter: Yes
App-only authentication: https://api.twitter.com/oauth2/token
Request token URL: https://api.twitter.com/oauth/request_token
Authorize URL: https://api.twitter.com/oauth/authorize
Access token URL: https://api.twitter.com/oauth/access_token

where is the problem?
 

Periklis Koutsogiannis

Active Member
Licensed User
I used your callback url and it works fine.

1. Reinstall the app and try again
2. Try to run the sample with your credentials on an other device
3. Regenerate API Keys and tokens in the twitter dev console
3. Create a new app with new details and try to see if it works fine.
 

brelto85

Active Member
Licensed User
Now i've another problem
To allow the tweet from this library, the permission must be set in "Read & Write" mode?

here my error after called the Tweet method. The provider is properly connected
(Result) {Canceled:false ,Message:TwitterException: 401 -1 null null ,Code:0 ,ErrorSubCode:0 ,Type: ,Map:null}}
 
Top