B4A Tutorial Twitter - V1.1 API example (application-only auth)

Discussion in 'Tutorials & Examples' started by Erel, Jun 19, 2013.

  1. Erel

    Erel Administrator Staff Member Licensed User

    [​IMG]

    This example is based on the new Twitter API. The previous one (V1.0) is no longer working.

    In the new API all the requests must be authenticated. There are two types of authentication methods. This example is based on the application-only auth method.

    In this mode the user do not need to have a Twitter account and doesn't need to sign in.

    Before you can use it you should get an app key and app secret values from twitter and set them in the code. This is done by registering your app with Twitter: https://dev.twitter.com/apps

    You need to get the two consumer values:

    [​IMG]

    This project implements a mini-Twitter client which follows a single user. The app will check for new tweets every 15 minutes and will show a notification when such are available.

    Frameworks used in this example:
    HttpUtils2 - Interacting with Twitter API
    CustomListView - Show the messages. Unlike ListView the items height can be different for different items. It will be set automatically based on the message length.
    KeyValueStore - Makes it simple to store all kinds of objects, such as the list of messages.

    It is a good example for interaction between a service and an activity.

    Note that if you change the package name then you need to set the CustomView type in the designer as it will refer the old type.
     

    Attached Files:

    Adro_Sydney likes this.
  2. bit

    bit New Member Licensed User

    need help

    i tried this example but i get an error like this
     

    Attached Files:

  3. Erel

    Erel Administrator Staff Member Licensed User

    This is strange. Tweet type is declared in the TwitterService module. Do you see this declaration?
     
  4. wexican

    wexican Member Licensed User

    Hi Erel,

    I've tried the above example & it works perfect to logon & retrieve tweets from a particular user, using the above code how can I modify it to logon in the same way & send a basic tweet with the push of a single button?

    Thanks,

    Wexican.

    Tested on HTC One X!
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    The application-only mode doesn't allow you to add tweet (no user account it used in this case).
     
  6. wexican

    wexican Member Licensed User

    Thanks for the reply Erel,

    If I create a twitter account is there an easy way to post tweets?
    I was thinking of using a webview, login with an account details, then manipulate the website with javascript through the webview to send tweets, but if the website changes my b4a app may fail to login etc.

    I've done this with Vodafone website before, but looking for an easier & more robust way.

    Thanks,

    Wexican
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    It should be possible to implement the other Twitter protocols. There was another example however it is based on an API no longer supported by Twitter.
     
  8. BowTieNeck

    BowTieNeck Member Licensed User

    Hi Erel,

    Thanks for this example. I'm using it to read the twitter feeds of just one twitter user (my local independent cinema).

    What I'd like to do is just access the twitter feeds once, instead of every 15 minutes.

    I tried putting CancelScheduledService(TwitterService) in the Main.Pause sub but when I come out of the app and look in Task manager, the app is still using cpu.

    I also put StopService(Me) in the TwitterService.Service_Destroy sub.

    And I tried StartService instead of StartServiceAt in TwitterService.Service_Start but that appeared to loop.

    Any ideas?

    Thanks,
    Chris
     
  9. Erel

    Erel Administrator Staff Member Licensed User

    Just remove ServiceStartAt from Service_Start event.
     
  10. BowTieNeck

    BowTieNeck Member Licensed User

    Thanks Erel, that works perfectly. I obviously don't yet understand services properly. But I'm working on it.
     
  11. ferdztech

    ferdztech Member Licensed User

    How to post tweets in new API v1.1 ,badly need it ..tnx..
     
  12. somed3v3loper

    somed3v3loper Active Member Licensed User

    Can you please post a function to get access token using api 1.1?
    I have been looking for an easy tutorial for days to get it working but I am not good at languages I found examples for.
    Please post a B4A Function :)
     
  13. Erel

    Erel Administrator Staff Member Licensed User

    See how the token is extracted in the example above.
     
  14. somed3v3loper

    somed3v3loper Active Member Licensed User

    Thank you . But is this access token capable of doing other functions like tweet , retweet and follow?
     
  15. Erel

    Erel Administrator Staff Member Licensed User

  16. somed3v3loper

    somed3v3loper Active Member Licensed User

    I am sorry for annoying you . Can you please tell me how to get the right access token?
    Thank you in advance
     
  17. somed3v3loper

    somed3v3loper Active Member Licensed User

    I tried to modify your function to get the right access token but could not succeed . here is my horrible code
    Code:
    Public Sub GetToken '(rd As RequestData)
        Dim tok As HttpJob
        tok.Initialize(
    "tok", Me)
    '    Dim fullkey As String = Main.AppKey.Trim & ":" & Main.AppSecret.Trim
        Dim su As StringUtils
        
    Dim auth As String="OAuth oauth_nonce=K7ny27JTpKVsTgdyLdDfmQQWVLERj2zAK5BslRsqyw,oauth_callback="&main.cb&", oauth_signature_method="&"~HMAC-SHA1~".Replace("~",QUOTE)&", oauth_timestamp="&DateTime.Now&", oauth_consumer_key="&main.AppKey.Trim&", oauth_signature="&"~Pc%2BMLdv028fxCErFyi8KXFM%2BddU%3D~, oauth_version=~1.0~".Replace("~",QUOTE)
        
    Dim b64 As String = su.EncodeBase64(auth.GetBytes("UTF-8"))
        tok.PostString(
    "https://api.twitter.com/oauth/request_token""oauth_callback="&Main.cb)
        tok.GetRequest.SetHeader(
    "Authorization", b64)
    '    tok.Tag = rd
    End Sub
    Can you or anyone help me?
     
  18. Erel

    Erel Administrator Staff Member Licensed User

    Where is the documentation of this API?
     
  19. somed3v3loper

    somed3v3loper Active Member Licensed User

    Here is the link

    https://dev.twitter.com/docs/api/1/post/oauth/request_token

    I am still modifying the function but with no success , actually I do not think I fully understood the new api 1.1

    Code:
    Public Sub GetToken '(rd As RequestData)
        Dim tok As HttpJob
        tok.Initialize(
    "tok", Me)
    '    Dim fullkey As String = Main.AppKey.Trim & ":" & Main.AppSecret.Trim
        Dim su As StringUtils
        
    Dim baseurl As String="https://api.twitter.com/oauth/request_token"
        
    Dim parameterstring As String="OAuth oauth_nonce=K7ny27JTpKVsTgdyLdDfmQQWVLERj2zAK5BGyZeRyw&"&"oauth_callback="&Main.cb&"&oauth_signature_method="&"~HMAC-SHA1~".Replace("~",QUOTE)&"&oauth_timestamp="&DateTime.Now&"&oauth_consumer_key="&Main.AppKey.Trim&"& oauth_version=~1.0~".Replace("~",QUOTE)
        
    Dim suu As StringUtils
       
        
    Dim signaturebasestring As String="POST&"&suu.EncodeUrl(baseurl,"ASCII")&"&"&suu.EncodeUrl(parameterstring,"ASCII")
       
        
    Dim signingkey As String=suu.EncodeUrl(Main.AppSecret.Trim,"ASCII")&"&"'&suu.EncodeUrl(main.AppSecret.Trim,"ASCII")
        Dim sinature As String=CreateHash("HMAC-SHA1",signaturebasestring&signingkey,"UTF-8")
        
    Log(sinature)
        
    Dim auth As String="OAuth oauth_nonce=K7ny27JTpKVsTgdyLdDfmQQWVLERj2zAK5BGyZeRyw,oauth_callback="&Main.cb&", oauth_signature_method="&"~HMAC-SHA1~".Replace("~",QUOTE)&", oauth_timestamp="&DateTime.Now&", oauth_consumer_key="&Main.AppKey.Trim&", oauth_signature="&sinature&", oauth_version=~1.0~".Replace("~",QUOTE)
        
    Dim b64 As String = su.EncodeBase64(auth.GetBytes("UTF-8"))
        tok.PostString(baseurl, Main.cb)
        tok.GetRequest.SetHeader(
    "Authorization", b64)
    '    tok.Tag = rd
    End Sub
     
  20. Erel

    Erel Administrator Staff Member Licensed User

Share This Page

Loading...