Xojo Conferences
XDCMay2019MiamiUSA

Platforms to show: All Mac Windows Linux Cross-Platform

/CURL/CURLS post twitter with OAuth/CURLS post twitter with OAuth v5
Function:
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /CURL/CURLS post twitter with OAuth/CURLS post twitter with OAuth v5
This example is the version from Mon, 24th Sep 2017.
Project "CURLS post twitter with OAuth v5.rbp"
FileTypes
Filetype text
Filetype all
End FileTypes
Class Window1 Inherits Window
Control PathText Inherits Label
ControlInstance PathText Inherits Label
End Control
Control ListBox1 Inherits ListBox
ControlInstance ListBox1 Inherits ListBox
End Control
Control URL Inherits TextField
ControlInstance URL Inherits TextField
End Control
Control StaticText2 Inherits Label
ControlInstance StaticText2 Inherits Label
End Control
Control PushButton2 Inherits PushButton
ControlInstance PushButton2 Inherits PushButton
EventHandler Sub Action() if gOAuth_Access_Token_Secret="" then Beep Window2.Show return end if DoTweet(Message.text,mFile) Message.text="" PathText.text="" mFile=Nil return End EventHandler
End Control
Control Result Inherits TextField
ControlInstance Result Inherits TextField
End Control
Control StaticText4 Inherits Label
ControlInstance StaticText4 Inherits Label
End Control
Control message Inherits TextArea
ControlInstance message Inherits TextArea
End Control
Control PushButton3 Inherits PushButton
ControlInstance PushButton3 Inherits PushButton
EventHandler Sub Action() mFile=GetOpenFolderItem("all") if mFile=Nil then PathText.Text="" mFile=Nil return end if if mFile.name.right(4) = ".jpg" then PathText.Text=mFile.UnixpathMBS else PathText.Text="" mFile=Nil end if End EventHandler
End Control
Control TwitPic_API_KEY Inherits TextField
ControlInstance TwitPic_API_KEY Inherits TextField
EventHandler Sub Open() gTwitPic_Api_Key=me.Text End EventHandler
EventHandler Sub TextChange() gTwitPic_Api_Key=Me.Text End EventHandler
End Control
Control StaticText3 Inherits Label
ControlInstance StaticText3 Inherits Label
End Control
Control PushButton5 Inherits PushButton
ControlInstance PushButton5 Inherits PushButton
EventHandler Sub Action() Dim f As FolderItem=FindFile("oauthdata.txt") Dim ts As TextOutputStream=TextOutputStream.Create(f) if gHttpProxyUse then ts.WriteLine "1" else ts.WriteLine "0" end if ts.WriteLine gHttpProxyAdress ts.WriteLine Str(gHttpProxyPort) ts.WriteLine gConsumer_Key ts.WriteLine gConsumer_Secret ts.WriteLine gScreen_Name ts.WriteLine gOAuth_Verifier ts.WriteLine gOAuth_Access_Token ts.WriteLine gOAuth_Access_Token_Secret ts.WriteLine gTwitPic_Api_Key ts.Close End EventHandler
End Control
Control apiver Inherits PopupMenu
ControlInstance apiver Inherits PopupMenu
EventHandler Sub Change() url.Text=Me.Text End EventHandler
End Control
Control StaticText5 Inherits Label
ControlInstance StaticText5 Inherits Label
End Control
Control StaticText6 Inherits Label
ControlInstance StaticText6 Inherits Label
End Control
Control StaticText7 Inherits Label
ControlInstance StaticText7 Inherits Label
End Control
Control StaticText8 Inherits Label
ControlInstance StaticText8 Inherits Label
End Control
Control PushButton6 Inherits PushButton
ControlInstance PushButton6 Inherits PushButton
EventHandler Sub Action() ShowURL "http://dev.twitpic.com" End EventHandler
End Control
Private Sub DoUploadV1(f As FolderItem, m As String) dim e as integer dim d as UploadCURL const CURLFORM_NOTHING = 0 const CURLFORM_COPYNAME = 1 const CURLFORM_PTRNAME = 2 const CURLFORM_NAMELENGTH = 3 const CURLFORM_COPYCONTENTS = 4 const CURLFORM_PTRCONTENTS = 5 const CURLFORM_CONTENTSLENGTH = 6 const CURLFORM_FILECONTENT = 7 const CURLFORM_ARRAY = 8 const CURLFORM_FILE = 10 const CURLFORM_BUFFER = 11 const CURLFORM_BUFFERPTR = 12 const CURLFORM_BUFFERLENGTH = 13 const CURLFORM_CONTENTTYPE = 14 const CURLFORM_CONTENTHEADER = 15 const CURLFORM_FILENAME = 16 d=new UploadCURL d.OptionURL=url.text d.OptionVerbose=True // add a section named filMyFile // with a file named file.name // and binary content in buf d.FormAdd(CURLFORM_COPYNAME, "consumer_token", CURLFORM_COPYCONTENTS, gConsumer_Key) d.FormAdd(CURLFORM_COPYNAME, "consumer_secret", CURLFORM_COPYCONTENTS, gConsumer_Secret) d.FormAdd(CURLFORM_COPYNAME, "oauth_token", CURLFORM_COPYCONTENTS, gOAuth_Access_Token) d.FormAdd(CURLFORM_COPYNAME, "oauth_secret", CURLFORM_COPYCONTENTS, gOAuth_Access_Token_Secret) if m<>"" then d.FormAdd(CURLFORM_COPYNAME, "message", CURLFORM_COPYCONTENTS, m) d.FormAdd(CURLFORM_COPYNAME, "key", CURLFORM_COPYCONTENTS, gTwitPic_Api_Key) d.FormAdd(CURLFORM_COPYNAME, "media", CURLFORM_FILE, f.UnixpathMBS) d.FormFinish if gHttpProxyUse then//Proxy d.OptionProxy=gHttpProxyAdress d.OptionProxyPort=gHttpProxyPort end if dim header(-1) as string //header.Append "Content-Type: text/xml" header.Append "application/x-www-form-urlencoded" header.Append "" d.SetOptionHTTPHeader header e=d.Perform ListBox1.AddRow "Result: "+str(e) ListBox1.AddRow d.GetInfoContentType dim r as string = d.result Result.text=r End Sub
Private Sub DoUploadV1Shell(f As FolderItem, m As String) Dim s As String s="curl" if gHttpProxyUse then//Proxy s=s+" -x "+gHttpProxyAdress+":"+Str(gHttpProxyPort) end if s=s+" -v" // -F "consumer_token=vTQYfsgNmyA0fDXkVtuRw" s=s+" -F ""consumer_token="+gConsumer_Key+"""" s=s+" -F ""consumer_secret="+gConsumer_Secret+"""" s=s+" -F ""oauth_token="+gOAuth_Access_Token+"""" s=s+" -F ""oauth_secret="+gOAuth_Access_Token_Secret+"""" if m<>"" then s=s+" -F ""message="+m+""""//EncodeURLComponent(Message.text) s=s+" -F ""key="+gTwitPic_Api_Key+"""" s=s+" -F ""media=@"+f.UnixpathMBS+"""" s=s+" "+url.Text Dim cb As New Clipboard cb.Text=s cb.Close Dim e As New Shell e.Execute(s) 'ListBox1.AddRow "Result: "+str(e) 'ListBox1.AddRow d.GetInfoContentType dim r as string = e.Result Result.text=r End Sub
Private Sub DoUploadV2(f As FolderItem, m As String) dim e as integer dim d as UploadCURL dim h(0) as string Dim str1,str2,str3 As String Dim CONSUMER_KEY As String=gConsumer_Key Dim CONSUMER_SECRET As String= gConsumer_Secret Dim ACCESS_TOKEN As String=gOAuth_Access_Token Dim ACCESS_TOKEN_SECRET As String=gOAuth_Access_Token_Secret Dim NONCE As String=RandomString(32) Dim TIMESTAMP As String=MakeTimeStamp str1="https://api.twitter.com/1/account/verify_credentials.json" str1=EncodeURLComponent(str1) str1="GET"+"&"+str1+"&" str2=str2+"oauth_consumer_key="+CONSUMER_KEY str2=str2+"&oauth_nonce="+NONCE str2=str2+"&oauth_signature_method=HMAC-SHA1" str2=str2+"&oauth_timestamp="+TIMESTAMP str2=str2+"&oauth_token="+ACCESS_TOKEN str2=str2+"&oauth_version=1.0" str3=CONSUMER_SECRET+"&"+ACCESS_TOKEN_SECRET Dim signature_base_string As String = str1+EncodeURLComponent(str2) Dim sha1class As New SHA1MBS Dim hmacsha1 As String= sha1class.HMAC(str3,signature_base_string) Dim signature_string As String = EncodeBase64(hmacsha1) signature_string = EncodeURLComponent(signature_string) h.Append "X-Verify-Credentials-Authorization: OAuth realm=""http://api.twitter.com/"", "+_ "oauth_consumer_key="""+CONSUMER_KEY+""", "+_ "oauth_signature_method=""HMAC-SHA1"", "+_ "oauth_token="""+ACCESS_TOKEN+""", "+_ "oauth_timestamp="""+TIMESTAMP+""", "+_ "oauth_nonce="""+NONCE+""", "+_ "oauth_version=""1.0"", "+_ "oauth_signature="""+signature_string+"""" h.Append "X-Auth-Service-Provider: https://api.twitter.com/1/account/verify_credentials.json" const CURLFORM_NOTHING = 0 const CURLFORM_COPYNAME = 1 const CURLFORM_PTRNAME = 2 const CURLFORM_NAMELENGTH = 3 const CURLFORM_COPYCONTENTS = 4 const CURLFORM_PTRCONTENTS = 5 const CURLFORM_CONTENTSLENGTH = 6 const CURLFORM_FILECONTENT = 7 const CURLFORM_ARRAY = 8 const CURLFORM_FILE = 10 const CURLFORM_BUFFER = 11 const CURLFORM_BUFFERPTR = 12 const CURLFORM_BUFFERLENGTH = 13 const CURLFORM_CONTENTTYPE = 14 const CURLFORM_CONTENTHEADER = 15 const CURLFORM_FILENAME = 16 d=new UploadCURL d.OptionURL=url.text d.OptionVerbose=True d.SetOptionHTTPHeader h // add a section named filMyFile // with a file named file.name // and binary content in buf d.FormAdd(CURLFORM_COPYNAME, "key", CURLFORM_COPYCONTENTS, gTwitPic_Api_Key) d.FormAdd(CURLFORM_COPYNAME, "media", CURLFORM_FILE, f.UnixpathMBS) if m<>"" then d.FormAdd(CURLFORM_COPYNAME, "message", CURLFORM_COPYCONTENTS, m) d.FormFinish 'dim b as BinaryStream 'b=mFile.OpenAsBinaryFile(false) 'd.data=b.Read(b.Length) 'd.OptionUpload=true 'd.OptionInFileSize=b.Length if gHttpProxyUse then//Proxy d.OptionProxy=gHttpProxyAdress d.OptionProxyPort=gHttpProxyPort end if e=d.Perform ListBox1.AddRow "Result: "+str(e) ListBox1.AddRow d.GetInfoContentType dim r as string = d.result Result.text=r End Sub
Private Sub DoUploadV2Shell(f As FolderItem, m As String) Dim str1,str2,str3 As String Dim CONSUMER_KEY As String=gConsumer_Key Dim CONSUMER_SECRET As String= gConsumer_Secret Dim ACCESS_TOKEN As String=gOAuth_Access_Token Dim ACCESS_TOKEN_SECRET As String=gOAuth_Access_Token_Secret Dim nonce As String=RandomString(32) Dim timestamp As String=MakeTimeStamp str1="https://api.twitter.com/1/account/verify_credentials.json" str1=EncodeURLComponent(str1) str1="GET"+"&"+str1+"&" str2=str2+"oauth_consumer_key="+CONSUMER_KEY str2=str2+"&oauth_nonce="+NONCE str2=str2+"&oauth_signature_method=HMAC-SHA1" str2=str2+"&oauth_timestamp="+TIMESTAMP str2=str2+"&oauth_token="+ACCESS_TOKEN str2=str2+"&oauth_version=1.0" str3=CONSUMER_SECRET+"&"+ACCESS_TOKEN_SECRET Dim signature_base_string As String = str1+EncodeURLComponent(str2) Dim sha1class As New SHA1MBS Dim hmacsha1 As String= sha1class.HMAC(str3,signature_base_string) Dim signature_string As String = EncodeBase64(hmacsha1) signature_string = EncodeURLComponent(signature_string) Dim s As String s="curl" if gHttpProxyUse then//Proxy s=s+" -x "+gHttpProxyAdress+":"+Str(gHttpProxyPort) end if s=s+" -v" s=s+" -H 'X-Auth-Service-Provider: https://api.twitter.com/1/account/verify_credentials.json'" s=s+" -H 'X-Verify-Credentials-Authorization: OAuth realm=""http://api.twitter.com/"", "+_ "oauth_consumer_key="""+gConsumer_Key+""", "+_ "oauth_signature_method=""HMAC-SHA1"", "+_ "oauth_token="""+gOAuth_Access_Token+""", "+_ "oauth_timestamp="""+timestamp+""", "+_ "oauth_nonce="""+nonce+""", "+_ "oauth_version=""1.0"", "+_ "oauth_signature="""+signature_string+"""'" // -F "consumer_token=vTQYfsgNmyA0fDXkVtuRw" if m<>"" then s=s+" -F ""message="+m+"""" s=s+" -F ""key="+gTwitPic_Api_Key+"""" s=s+" -F ""media=@"+f.UnixpathMBS+"""" s=s+" "+url.Text Dim cb As New Clipboard cb.Text=s cb.Close Dim e As New Shell e.Execute(s) 'ListBox1.AddRow "Result: "+str(e) 'ListBox1.AddRow d.GetInfoContentType dim r as string = e.Result Result.text=r End Sub
Property mFile As FolderItem
End Class
Class Window2 Inherits Window
Control StaticText1 Inherits Label
ControlInstance StaticText1 Inherits Label
End Control
Control StaticText2 Inherits Label
ControlInstance StaticText2 Inherits Label
End Control
Control CONSUMER_KEY Inherits TextField
ControlInstance CONSUMER_KEY Inherits TextField
End Control
Control CONSUMER_SECRET Inherits TextField
ControlInstance CONSUMER_SECRET Inherits TextField
End Control
Control PushButton1 Inherits PushButton
ControlInstance PushButton1 Inherits PushButton
EventHandler Sub Action() gConsumer_Key=CONSUMER_KEY.Text gConsumer_Secret=CONSUMER_SECRET.Text Call RequestToken(rqstTknMthdPM.ListIndex) Result1.Text="Result = "+Str(gOAuthStatus) Request_Token.Text=gOAuth_Request_Token Request_Token_Secret.Text=gOAuth_Request_Token_Secret End EventHandler
End Control
Control CheckBox1 Inherits CheckBox
ControlInstance CheckBox1 Inherits CheckBox
EventHandler Sub Action() gHttpProxyUse=Me.Value End EventHandler
End Control
Control TextField3 Inherits TextField
ControlInstance TextField3 Inherits TextField
EventHandler Sub TextChange() gHttpProxyAdress=Me.Text End EventHandler
End Control
Control TextField4 Inherits TextField
ControlInstance TextField4 Inherits TextField
EventHandler Sub TextChange() gHttpProxyPort=Val(me.Text) End EventHandler
End Control
Control StaticText3 Inherits Label
ControlInstance StaticText3 Inherits Label
End Control
Control StaticText4 Inherits Label
ControlInstance StaticText4 Inherits Label
End Control
Control StaticText5 Inherits Label
ControlInstance StaticText5 Inherits Label
End Control
Control StaticText6 Inherits Label
ControlInstance StaticText6 Inherits Label
End Control
Control Request_Token Inherits TextField
ControlInstance Request_Token Inherits TextField
End Control
Control Request_Token_Secret Inherits TextField
ControlInstance Request_Token_Secret Inherits TextField
End Control
Control StaticText7 Inherits Label
ControlInstance StaticText7 Inherits Label
End Control
Control Result1 Inherits Label
ControlInstance Result1 Inherits Label
End Control
Control PushButton2 Inherits PushButton
ControlInstance PushButton2 Inherits PushButton
EventHandler Sub Action() hv.LoadURL "http://api.twitter.com/oauth/authorize?oauth_token="+gOAuth_Request_Token End EventHandler
End Control
Control StaticText8 Inherits Label
ControlInstance StaticText8 Inherits Label
End Control
Control Result2 Inherits Label
ControlInstance Result2 Inherits Label
End Control
Control hv Inherits HTMLViewer
ControlInstance hv Inherits HTMLViewer
EventHandler Sub DocumentComplete(URL as String) Dim s As String #if TargetWin32 s=Me.IEHTMLTextMBS//Me.IETextMBS// #else if Me.mainFrameMBS<>Nil then if Me.mainFrameMBS.dataSource<>Nil then s=Me.mainFrameMBS.dataSource.data end if end if #endif s=GetOAuthPin(s) if s<>"" then gOAuth_Verifier=s Call AccessToken(acsTknMthdPM.ListIndex) Result2.Text="Result = "+Str(gOAuthStatus) Account.Text=gScreen_Name Verifier.Text=gOAuth_Verifier Access_Token.Text=gOAuth_Access_Token Access_Token_Secret.Text=gOAuth_Access_Token_Secret Window1.Show end if End EventHandler
End Control
Control StaticText9 Inherits Label
ControlInstance StaticText9 Inherits Label
End Control
Control StaticText10 Inherits Label
ControlInstance StaticText10 Inherits Label
End Control
Control Access_Token Inherits TextField
ControlInstance Access_Token Inherits TextField
End Control
Control Access_Token_Secret Inherits TextField
ControlInstance Access_Token_Secret Inherits TextField
End Control
Control StaticText11 Inherits Label
ControlInstance StaticText11 Inherits Label
End Control
Control StaticText12 Inherits Label
ControlInstance StaticText12 Inherits Label
End Control
Control Account Inherits TextField
ControlInstance Account Inherits TextField
End Control
Control Verifier Inherits TextField
ControlInstance Verifier Inherits TextField
End Control
Control PushButton3 Inherits PushButton
ControlInstance PushButton3 Inherits PushButton
EventHandler Sub Action() Dim f As FolderItem=FindFile("oauthdata.txt") if f.Exists=false then beep MsgBox "not found oauth data... push step 1 button..." Return end if Dim ts As TextInputStream=TextInputStream.Open(f) gHttpProxyUse=false if ts.ReadLine="1" then gHttpProxyUse=true gHttpProxyAdress=ts.ReadLine gHttpProxyPort=Val(ts.ReadLine) gConsumer_Key=ts.ReadLine gConsumer_Secret=ts.ReadLine gScreen_Name=ts.ReadLine gOAuth_Verifier=ts.ReadLine gOAuth_Access_Token=ts.ReadLine gOAuth_Access_Token_Secret=ts.ReadLine gTwitPic_Api_Key=ts.ReadLine ts.Close CheckBox1.Value=gHttpProxyUse TextField3.Text=gHttpProxyAdress TextField4.Text=Str(gHttpProxyPort) CONSUMER_KEY.Text=gConsumer_Key CONSUMER_SECRET.Text=gConsumer_Secret Account.Text=gScreen_Name Verifier.Text=gOAuth_Verifier Access_Token.Text=gOAuth_Access_Token Access_Token_Secret.Text=gOAuth_Access_Token_Secret Window1.Show Window1.TwitPic_API_KEY.Text=gTwitPic_Api_Key End EventHandler
End Control
Control StaticText13 Inherits Label
ControlInstance StaticText13 Inherits Label
End Control
Control PushButton4 Inherits PushButton
ControlInstance PushButton4 Inherits PushButton
EventHandler Sub Action() ShowURL "http://twitter.com/apps" End EventHandler
End Control
Control StaticText14 Inherits Label
ControlInstance StaticText14 Inherits Label
End Control
Control rqstTknMthdPM Inherits PopupMenu
ControlInstance rqstTknMthdPM Inherits PopupMenu
End Control
Control acsTknMthdPM Inherits PopupMenu
ControlInstance acsTknMthdPM Inherits PopupMenu
End Control
End Class
Class Window3 Inherits Window
Control TextArea1 Inherits TextArea
ControlInstance TextArea1 Inherits TextArea
End Control
End Class
MenuBar MenuBar1
MenuItem UntitledMenu1 = ""
MenuItem FileMenu = "&File"
MenuItem FileQuit = "Quit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem UntitledMenu0 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "Clear"
End MenuBar
Class App Inherits Application
EventHandler Sub Open() //Window1.Show Window2.Show End EventHandler
End Class
Class UploadCURL Inherits CURLSMBS
EventHandler Sub DebugMessage(infotype as integer, data as string, dataSize as Integer) System.DebugLog str(infotype) System.DebugLog data End EventHandler
EventHandler Function Progress(dltotal as Int64, dlnow as Int64, ultotal as Int64, ulnow as Int64, percent as double) As boolean 'if uplist<>Nil then 'if ultotal=0 then 'uplist.AddRow "Uploading..." 'else 'uplist.AddRow "Uploading "+Format(ulnow/ultotal,"0%") 'end if 'end if End EventHandler
EventHandler Function Write(data as string, dataSize as Integer) As integer result=result+data Return lenb(data) End EventHandler
Sub Constructor() End Sub
Property result As string
Property uplist As Listbox
End Class
Class MyCURL Inherits CURLSMBS
EventHandler Sub DebugMessage(infotype as integer, data as string, dataSize as Integer) System.DebugLog data End EventHandler
End Class
Module OAuthUtil
Const kTwitpicV1 = 0
Const kTwitpicV2 = 1
Function AccessToken(tknMthd As Integer) As Integer Dim tokenURL As String ="https://api.twitter.com/oauth/access_token" Select Case tknMthd Case 1 tokenURL="https://api.twitter.com/oauth/access_token" End Select Dim str1,str2,str3 As String str1=tokenURL str1=EncodeURLComponent(str1) str1="POST&"+str1+"&" str2="oauth_consumer_key="+gConsumer_Key str2=str2+"&oauth_nonce="+RandomString(32) str2=str2+"&oauth_signature_method=HMAC-SHA1" str2=str2+"&oauth_timestamp="+MakeTimeStamp//1274401561 str2=str2+"&oauth_token="+gOAuth_Request_Token str2=str2+"&oauth_verifier="+gOAuth_Verifier str2=str2+"&oauth_version=1.0" str3=gConsumer_Secret+"&"+gOAuth_Request_Token_Secret//oauth_consumer_secret Dim signature_base_string As String = str1+EncodeURLComponent(str2) Dim sha1class As New SHA1MBS Dim hmacsha1 As String= sha1class.HMAC(str3,signature_base_string) Dim signature_string As String = EncodeBase64(hmacsha1) signature_string = EncodeURLComponent(signature_string) Dim res As String Select Case tknMthd Case 1//Socket Dim hsock as new HTTPSecureSocket hsock.Secure = true if gHttpProxyUse then hsock.HTTPProxyAddress=gHttpProxyAdress hsock.HTTPProxyPort=gHttpProxyPort end if Dim str4 as String str4="OAuth realm=""OAuth"", " str4=str4+ReplaceAll(ReplaceAll(str2,"&",""", "),"=","=""")+", " str4=str4+ "oauth_signature="""+signature_string+"""" hsock.SetRequestHeader("Authorization", str4) res=hsock.Post(tokenURL, 20) gOAuthStatus=hsock.ErrorCode Else//CURL dim header(-1) as string dim c as new MyCURL c.OptionVerbose=true c.OptionURL = tokenURL+"?"+str2+"&oauth_signature="+signature_string c.OptionCustomRequest="POST" if gHttpProxyUse then c.OptionProxy=gHttpProxyAdress c.OptionProxyPort=gHttpProxyPort end if //header.Append "Content-Type: text/xml" header.Append "application/x-www-form-urlencoded" header.Append "" c.SetOptionHTTPHeader header gOAuthStatus=c.Perform res=c.OutputData End Select gOAuth_Access_Token=NthField(NthField(res,"oauth_token=",2),"&",1) gOAuth_Access_Token_Secret=NthField(NthField(res,"oauth_token_secret=",2),"&",1) gScreen_Name=NthField(NthField(res,"screen_name=",2),"&",1) Return gOAuthStatus End Function
Function DecodeUnicodeString(s As String) As String 'dim h as string = "30c8" 'dim s as string = encodings.utf8.chr(val("&h"+h)) Dim t As String if Instr(s,"\u")>0 then do if MidB(s,1,2)="\u" then t=t+encodings.utf8.chr(val("&h"+MidB(s,3,4))) s=MidB(s,7) else t=t+MidB(s,1,1) s=MidB(s,2) end if Loop Until s="" return t end if return s End Function
Sub DoTweet(m As String, f As FolderItem, v As Integer=kTwitpicV2) if f<>Nil then Dim d As UploadCURL=GetTwitPicCURL(f,m,v) gOAuthStatus=d.Perform //Post for v2 m=""//erase message if v=kTwitpicV2 then m=GetPostMsgV2(d.result) end if if m<>"" then //post only or twitpic api v2 Dim c As MyCURL=GetTwitterCURL("POST","http://twitter.com/statuses/update.xml","","status="+EncodeURLComponent(m)) gOAuthStatus=c.Perform end if End Sub
Function FindFile(name as string) As FolderItem // Look for file in parent folders from executable on dim parent as FolderItem = app.ExecutableFile.Parent while parent<>Nil dim file as FolderItem = parent.Child(name) if file<>Nil and file.Exists then Return file end if parent = parent.Parent wend End Function
Function GetOAuthPin(s As String) As String Dim idoauthpin As String idoauthpin="<div id=""oauth_pin"">"//<div id="oauth_pin"> //<DIV id=oauth_pin>7954217 </DIV></DIV></DIV></DIV> if InStr(s,idoauthpin)<=0 then idoauthpin="<DIV id=oauth_pin>" s=ReplaceLineEndings(s,EndOfLine) s=ReplaceAll(s,EndOfLine,"") s=ReplaceAll(s,chr(9),"") s=Trim(NthField(NthField(s,idoauthpin,2),"</div>",1)) s=ReplaceAll(s," ","") //20110718 if InStrB(s,"<code>")>0 then s=NthField(s,"<code>",2) s=NthField(s,"</code>",1) end if return s '<div id="oauth_pin"> '0474650 '</div> End Function
Function GetPostMsgV2(r As String) As String if gOAuthStatus<>0 then return "" Dim m As String //Post for v2 Dim j,k,l As JSONMBS Dim u As String j=new JSONMBS(r) if j<>Nil then k=j.ChildNode While k<>Nil if k.ValueString="text" then l=k.ChildNode if l<>Nil then m=DecodeUnicodeString(l.ValueString) elseif k.ValueString="url" then l=k.ChildNode if l<>Nil then u=l.ValueString //ReplaceAll(l.Text,"\/","/") end if k=k.NextNode Wend end if if u<>"" then//url of twitpic m=(m+" "+u)//post data m=ReplaceAll(m,"\/","/") else m="" end if return m End Function
Function GetTwitPicCURL(f As FolderItem, m As String, v As Integer=kTwitpicV2) As UploadCURL Select Case v Case kTwitpicV1 //Upload and Post return GetTwitPicCURLV1(f,m) Case kTwitpicV2 //Upload only return GetTwitPicCURLV2(f,m) End Select End Function
Function GetTwitPicCURLV1(f As FolderItem, m As String) As UploadCURL dim d as UploadCURL const CURLFORM_NOTHING = 0 const CURLFORM_COPYNAME = 1 const CURLFORM_PTRNAME = 2 const CURLFORM_NAMELENGTH = 3 const CURLFORM_COPYCONTENTS = 4 const CURLFORM_PTRCONTENTS = 5 const CURLFORM_CONTENTSLENGTH = 6 const CURLFORM_FILECONTENT = 7 const CURLFORM_ARRAY = 8 const CURLFORM_FILE = 10 const CURLFORM_BUFFER = 11 const CURLFORM_BUFFERPTR = 12 const CURLFORM_BUFFERLENGTH = 13 const CURLFORM_CONTENTTYPE = 14 const CURLFORM_CONTENTHEADER = 15 const CURLFORM_FILENAME = 16 d=new UploadCURL d.OptionURL="http://api.twitpic.com/1/uploadAndPost.json" d.OptionVerbose=True // add a section named filMyFile // with a file named file.name // and binary content in buf d.FormAdd(CURLFORM_COPYNAME, "consumer_token", CURLFORM_COPYCONTENTS, gConsumer_Key) d.FormAdd(CURLFORM_COPYNAME, "consumer_secret", CURLFORM_COPYCONTENTS, gConsumer_Secret) d.FormAdd(CURLFORM_COPYNAME, "oauth_token", CURLFORM_COPYCONTENTS, gOAuth_Access_Token) d.FormAdd(CURLFORM_COPYNAME, "oauth_secret", CURLFORM_COPYCONTENTS, gOAuth_Access_Token_Secret) if m<>"" then d.FormAdd(CURLFORM_COPYNAME, "message", CURLFORM_COPYCONTENTS, m) d.FormAdd(CURLFORM_COPYNAME, "key", CURLFORM_COPYCONTENTS, gTwitPic_Api_Key) d.FormAdd(CURLFORM_COPYNAME, "media", CURLFORM_FILE, f.UnixpathMBS) d.FormFinish if gHttpProxyUse then//Proxy d.OptionProxy=gHttpProxyAdress d.OptionProxyPort=gHttpProxyPort end if dim header(-1) as string //header.Append "Content-Type: text/xml" header.Append "application/x-www-form-urlencoded" header.Append "" d.SetOptionHTTPHeader header return d 'e=d.Perform ' 'ListBox1.AddRow "Result: "+str(e) 'ListBox1.AddRow d.GetInfoContentType ' 'dim r as string = d.result ' 'Result.text=r End Function
Function GetTwitPicCURLV2(f As FolderItem, m As String) As UploadCURL dim d as UploadCURL dim h(0) as string Dim str1,str2,str3 As String Dim CONSUMER_KEY As String=gConsumer_Key Dim CONSUMER_SECRET As String= gConsumer_Secret Dim ACCESS_TOKEN As String=gOAuth_Access_Token Dim ACCESS_TOKEN_SECRET As String=gOAuth_Access_Token_Secret Dim NONCE As String=RandomString(32) Dim TIMESTAMP As String=MakeTimeStamp str1="https://api.twitter.com/1/account/verify_credentials.json" str1=EncodeURLComponent(str1) str1="GET"+"&"+str1+"&" str2=str2+"oauth_consumer_key="+CONSUMER_KEY str2=str2+"&oauth_nonce="+NONCE str2=str2+"&oauth_signature_method=HMAC-SHA1" str2=str2+"&oauth_timestamp="+TIMESTAMP str2=str2+"&oauth_token="+ACCESS_TOKEN str2=str2+"&oauth_version=1.0" str3=CONSUMER_SECRET+"&"+ACCESS_TOKEN_SECRET Dim signature_base_string As String = str1+EncodeURLComponent(str2) Dim sha1class As New SHA1MBS Dim hmacsha1 As String= sha1class.HMAC(str3,signature_base_string) Dim signature_string As String = EncodeBase64(hmacsha1) signature_string = EncodeURLComponent(signature_string) h.Append "X-Verify-Credentials-Authorization: OAuth realm=""http://api.twitter.com/"", "+_ "oauth_consumer_key="""+CONSUMER_KEY+""", "+_ "oauth_signature_method=""HMAC-SHA1"", "+_ "oauth_token="""+ACCESS_TOKEN+""", "+_ "oauth_timestamp="""+TIMESTAMP+""", "+_ "oauth_nonce="""+NONCE+""", "+_ "oauth_version=""1.0"", "+_ "oauth_signature="""+signature_string+"""" h.Append "X-Auth-Service-Provider: https://api.twitter.com/1/account/verify_credentials.json" const CURLFORM_NOTHING = 0 const CURLFORM_COPYNAME = 1 const CURLFORM_PTRNAME = 2 const CURLFORM_NAMELENGTH = 3 const CURLFORM_COPYCONTENTS = 4 const CURLFORM_PTRCONTENTS = 5 const CURLFORM_CONTENTSLENGTH = 6 const CURLFORM_FILECONTENT = 7 const CURLFORM_ARRAY = 8 const CURLFORM_FILE = 10 const CURLFORM_BUFFER = 11 const CURLFORM_BUFFERPTR = 12 const CURLFORM_BUFFERLENGTH = 13 const CURLFORM_CONTENTTYPE = 14 const CURLFORM_CONTENTHEADER = 15 const CURLFORM_FILENAME = 16 d=new UploadCURL d.OptionURL="http://api.twitpic.com/2/upload.json" d.OptionVerbose=True d.SetOptionHTTPHeader h // add a section named filMyFile // with a file named file.name // and binary content in buf d.FormAdd(CURLFORM_COPYNAME, "key", CURLFORM_COPYCONTENTS, gTwitPic_Api_Key) d.FormAdd(CURLFORM_COPYNAME, "media", CURLFORM_FILE, f.UnixpathMBS) if m<>"" then d.FormAdd(CURLFORM_COPYNAME, "message", CURLFORM_COPYCONTENTS, m) d.FormFinish 'dim b as BinaryStream 'b=mFile.OpenAsBinaryFile(false) 'd.data=b.Read(b.Length) 'd.OptionUpload=true 'd.OptionInFileSize=b.Length if gHttpProxyUse then//Proxy d.OptionProxy=gHttpProxyAdress d.OptionProxyPort=gHttpProxyPort end if return d 'e=d.Perform ' 'ListBox1.AddRow "Result: "+str(e) 'ListBox1.AddRow d.GetInfoContentType ' 'dim r as string = d.result ' 'Result.text=r End Function
Function GetTwitterCURL(type As String, api As String, optionA As String, optionB As String) As MyCURL Dim str1,str2,str3 As String str1=api str1=EncodeURLComponent(str1) str1=type+"&"+str1+"&" if optionA<>"" then str2=optionA+"&" str2=str2+"oauth_consumer_key="+gConsumer_Key str2=str2+"&oauth_nonce="+RandomString(32) str2=str2+"&oauth_signature_method=HMAC-SHA1" str2=str2+"&oauth_timestamp="+MakeTimeStamp//1274401561 str2=str2+"&oauth_token="+gOAuth_Access_Token str2=str2+"&oauth_version=1.0" if optionB<>"" then str2=str2+"&"+optionB str3=gConsumer_Secret+"&"+gOAuth_Access_Token_Secret Dim signature_base_string As String = str1+EncodeURLComponent(str2) Dim sha1class As New SHA1MBS Dim hmacsha1 As String= sha1class.HMAC(str3,signature_base_string) Dim signature_string As String = EncodeBase64(hmacsha1) signature_string = EncodeURLComponent(signature_string) dim header(-1) as string dim c as new MyCURL c.OptionVerbose=true c.OptionURL = api+"?"+str2+"&oauth_signature="+signature_string c.OptionCustomRequest=type//"POST" if gHttpProxyUse then//Proxy c.OptionProxy=gHttpProxyAdress c.OptionProxyPort=gHttpProxyPort end if //header.Append "Content-Type: text/xml" header.Append "application/x-www-form-urlencoded" header.Append "" c.SetOptionHTTPHeader header return c End Function
Function MakeTimeStamp() As String dim d as New date dim d2 as New Date(1970,1,1) dim timestamp as String d2.TotalSeconds = d2.TotalSeconds + (d2.GMTOffset * 3600) timestamp = Format((d.TotalSeconds - d2.TotalSeconds),"#") return timestamp End Function
Function RandomString(n As Integer) As String if n<1 then n=1 Dim i,v As Integer Dim s,t As String if gOAuthRandom=Nil then gOAuthRandom=New Random for i=1 to n v=gOAuthRandom.InRange(&h30,&h7A) t=chr(v) if v>&h39 and v<&h50 then t=chr(gOAuthRandom.InRange(&h30,&h39)) elseif v<&h61 then t=chr(gOAuthRandom.InRange(&h61,&h7A)) end if s=s+t next return s End Function
Function RequestToken(tknMthd As Integer) As Integer Dim tokenURL As String ="http://api.twitter.com/oauth/request_token" Select Case tknMthd Case 1 tokenURL="https://api.twitter.com/oauth/request_token" End Select Dim str1,str2,str3 As String str1 = tokenURL str1 = EncodeURLComponent(str1) str1 = "POST&" + str1 + "&" str2 = "oauth_consumer_key="+gConsumer_Key str2 = str2 + "&oauth_nonce="+RandomString(32) str2 = str2 + "&oauth_signature_method=HMAC-SHA1" str2 = str2 + "&oauth_timestamp="+MakeTimeStamp//1274401561 str2 = str2 + "&oauth_version=1.0" str3 = gConsumer_Secret+"&" Dim signature_base_string As String = str1 + EncodeURLComponent(str2) Dim sha1class As New SHA1MBS Dim hmacsha1 As String= sha1class.HMAC(str3,signature_base_string) Dim signature_string As String = EncodeBase64(hmacsha1) signature_string = EncodeURLComponent(signature_string) Dim res As String gOAuth_Request_Token="" gOAuth_Request_Token_Secret="" Select Case tknMthd Case 1//Socket Dim hsock as new HTTPSecureSocket hsock.Secure = true if gHttpProxyUse then hsock.HTTPProxyAddress=gHttpProxyAdress hsock.HTTPProxyPort=gHttpProxyPort end if Dim str4 as String str4="OAuth realm=""OAuth"", " str4=str4+ReplaceAll(ReplaceAll(str2,"&",""", "),"=","=""")+""", " str4=str4+"oauth_signature="""+signature_string+"""" hsock.SetRequestHeader("Authorization", str4) res=hsock.Post(tokenURL, 20) gOAuthStatus=hsock.ErrorCode Else//CURL dim header(-1) as string dim c as new MyCURL c.OptionVerbose=true c.OptionURL = tokenURL+"?" + str2 + "&oauth_signature=" + signature_string c.OptionCustomRequest="POST" if gHttpProxyUse then c.OptionProxy=gHttpProxyAdress c.OptionProxyPort=gHttpProxyPort end if //header.Append "Content-Type: text/xml" header.Append "application/x-www-form-urlencoded" header.Append "" c.SetOptionHTTPHeader header gOAuthStatus=c.Perform res=c.OutputData End Select gOAuth_Request_Token=NthField(NthField(res,"oauth_token=",2),"&",1) gOAuth_Request_Token_secret=NthField(NthField(res,"oauth_token_secret=",2),"&",1) Return gOAuthStatus End Function
Property gConsumer_Key As String
Property gConsumer_Secret As String
Property gHttpProxyAdress As String
Property gHttpProxyPort As Integer
Property gHttpProxyUse As Boolean
Property gOAuthRandom As Random
Property gOAuthStatus As Integer
Property gOAuth_Access_Token As String
Property gOAuth_Access_Token_Secret As String
Property gOAuth_Request_Token As String
Property gOAuth_Request_Token_Secret As String
Property gOAuth_Verifier As String
Property gScreen_Name As String
Property gTwitPic_Api_Key As String
End Module
End Project

See also:

Feedback, Comments & Corrections

The items on this page are in the following plugins: MBS CURL Plugin.




Links
MBS FileMaker blog