Android Question Help me with Twitter API 1.1

somed3v3loper

Well-Known Member
Licensed User
Longtime User
Hi
I try to follow this example
http://www.b4x.com/android/forum/threads/twitter-v1-1-api-example-application-only-auth.30382/

to get the required access token that give me more permissions (tweet , retweet , follow etc. ) but I have no success .
Here is one of my attempts but lacking knowledge in signing requests and using HMAC-SHA1 format made things harder:

B4X:
Public Sub GetToken '(rd As RequestData)
    Dim tok As HttpJob
    tok.Initialize("tok", Me)
    Dim baseurl As String="https://api.twitter.com/oauth/request_token"
    tok.PostString(baseurl,"oauth_callback="&Main.cb)
    Dim oauth_nonce As String="K7ny27JTpKVyxlqyLdDfmQQWVLERj2zAK5BslRsqyw"
    Dim oauth_consumer_key As String=Main.AppKey.Trim
    Dim httpmethod As String="POST"
    Dim oauth_signature As String
    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 tobeheader As String=suu.EncodeUrl(baseurl,"ASCII")&"&"&suu.EncodeUrl(parameterstring,"ASCII")
    CallSub2(Main,"msg",tobeheader)
    Dim signaturebasestring As String="POST&"&tobeheader
    LogColor(signaturebasestring,Colors.Blue)
    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,"ASCII")
    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", tobeheader)

End Sub

And here is the function I tried to generate HMAC-SHA1 format with

B4X:
Sub CreateHash(algorithm As String,data As String,charset As String)
  Dim m As Mac
  Dim k As KeyGenerator
  k.Initialize(algorithm)
  k.KeyFromBytes("key".GetBytes(charset))
  m.Initialise(algorithm, k.Key)
  m.Update(data.GetBytes(charset))
  Dim b() As Byte
  b = m.Sign
  Dim bc As ByteConverter
  Return bc.HexFromBytes(b)
End Sub

As I know almost nothing in these fields you can see they are just modifications of functions I got from the forum .

Please help me . Any library , class or function or example ?
Thanks in advance
 

Marco Nissen

Active Member
Licensed User
Longtime User
Well, now I understood that the OAuth/signpost implementation can be used in a fairly simple manner, at least for the one example I have from a oauth1.0a webservice. I didn't bother so much with twitter / facebook so much yet, because on iOS they are built in in the OS.

But: after I spend couple of days for implementing the oauth protocol from scratch I decided that this is worthless.
Better built on things other people already successfully developed ..
 
Upvote 0
Top