Instagram SDK Wrapper

Periklis Koutsogiannis

Active Member
Licensed User
Introduction

Used SocialApi classes:
  • InstagramProvider: 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 InstagramProvider object to an activity through the InstagramProvider.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\instagram\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.instagram.android.InstagramActivity"
        android:theme="@android:style/Theme.Translucent.NoTitleBar"/>)

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

3. Select the SocialApi and InstagramProvider libraries from the list of the available libraries in the IDE. Declare a InstagramProvider 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 Instagram As InstagramProvider
    Instagram.Initialize("<your-client-id>", "<your-client-secret> ", "<your-callback-url>")

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

5. Create your events
B4X:
Sub Instagram_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
    Instagram.Login
End Sub

Sub BtnDisconnect_Click
    Instagram.Logout
End Sub

8. Do something and evaluate the result
B4X:
Sub BtnRecentMedia_Click
    Dim Result As InstagramResult

    Result = Instagram.GetRecentMedia(1574083, 5) '1574083 = the id of "snoopdog", 0 = me
    If Result.Success Then
        Msgbox(Result.Map, "Snoop's recent media")
    End If
End Sub
Done! Now we have to setup our app in our Instagram developer dashboard.

Setup our app in Instagram developer dashboard

Follow the steps described here:
http://instagram.com/developer/register/

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 into a single package (socialapi) after finding out how to properly use the #AdditionalRes directive to avoid conflict with other b4a libraries
1.0
  • Initial version
 

Attachments

Last edited:

Jaames

Active Member
Licensed User
You are amazing man. Thanks for all of your effort, you just elevated the B4A to the higher level!
 

Periklis Koutsogiannis

Active Member
Licensed User

luke2012

Well-Known Member
Licensed User
So if we need to post a photo we can't do it with Instagram SDK wrapper, right ?
 
Top