Jump to content
Sign in to follow this  
phatzilla

Need help with Oauth / Twitter requests

Recommended Posts

phatzilla

Hi guys, im looking to update this script to work with Twitter POST requests (Such as make tweets / update friendships / change account settings)

I have this script currently (thanks to DeltaRocked).  The script works for GET requests (i have left in my authorization tokens/etc) however POST requests are formulated differently and im having immense trouble adapting the script.  Was wondering if anyone can work me through this?  Would be greatly appreciated and will send a tip via paypal for someone who offers up their time explaining it to me.

Cheers!

#include "WinHttp.au3"
#include <Crypt.au3>
#include <base64.au3>
#include <Date.au3>


$sTwitter_oAuth_Consumer_key = '3nVuSoBZnx6U4vzUxf5w'
$sTwitter_ConsumerSecret = 'Bcs59EFbbsdF6Sl9Ng71smgStWEGwXXKSjYvPVt7qys'
$sTwitter_oAuth_Token = '2413670697-F5Pq1AayK4w5sa42xt48yfww9UzqnAmcCzgnR8N'
$sTwitter_TokenSecret = 'qmCm6IGTPzb1LS08PJYOhbunp79MoW7Aa47SapEBjNL7M'



$header = gen_sig_base()
$header = StringSplit($header, '|', 2)



Local $arr = _WinHttpGetIEProxyConfigForCurrentUser()
If $arr[2] <> '' Then
    $hOpen = _WinHttpOpen('Mozilla/5.0', $WINHTTP_ACCESS_TYPE_NAMED_PROXY, $arr[2])
Else
    $hOpen = _WinHttpOpen('Mozilla/5.0')
EndIf


$hConnect = _WinHttpConnect($hOpen, "api.twitter.com", $INTERNET_DEFAULT_HTTPS_PORT)
$header[2] = URLEncode(_Base64Encode(BinaryToString($header[2])))



ConsoleWrite('epoch  ' & $header[0] & @CRLF)
ConsoleWrite('nonce  ' & $header[1] & @CRLF)
ConsoleWrite('signature ' & $header[2] & @CRLF)


$hRequestSSL = _WinHttpSimpleSendSSLRequest($hConnect, 'GET', "1.1/direct_messages.json?count=10", Default, Default, 'Authorization: OAuth ' _
         & 'oauth_consumer_key="' & $sTwitter_oAuth_Consumer_key & '", ' & 'oauth_nonce="' & $header[1] & '", ' _
         & 'oauth_signature="' & $header[2] & '", ' & 'oauth_signature_method="HMAC-SHA1", ' _
         & 'oauth_timestamp="' & $header[0] & '", ' & 'oauth_token="' & $sTwitter_oAuth_Token & '", ' _
         & 'oauth_version="1.0"')


$sReturned = _WinHttpSimpleReadData($hRequestSSL)
ConsoleWrite($sReturned & @CRLF)



_WinHttpCloseHandle($hRequestSSL)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)











Func gen_sig_base()
    Local $epoch, $nonce, $SignatureBase, $since_id, $Signature
    $tCur = _Date_Time_GetSystemTime()
    $date = _Date_Time_SystemTimeToDateTimeStr($tCur, 1)
    $epoch = _Epoch_encrypt($date)
    _Crypt_Startup()
    $nonce = StringTrimLeft(_Crypt_HashData($epoch & $sTwitter_oAuth_Consumer_key, $CALG_MD5), 2)
    _Crypt_Shutdown()
    $since_id = IniRead('Twitter.ini', 'TwitterAPI', 'since_id_dm', '')
    $SignatureBase = 'GET&https%3A%2F%2Fapi.twitter.com%2F1.1%2Fdirect_messages.json&count%3D10%26oauth_consumer_key%3D' & $sTwitter_oAuth_Consumer_key & '%26oauth_nonce%3D' & $nonce & '%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D' & $epoch & '%26oauth_token%3D' & $sTwitter_oAuth_Token & '%26oauth_version%3D1.0'
    $Signature = hmac($sTwitter_ConsumerSecret & '&' & $sTwitter_TokenSecret, $SignatureBase, 'sha1')
    Return $epoch & '|' & $nonce & '|' & $Signature
EndFunc   ;==>gen_sig_base
;~ By Trancexx



Func _Epoch_encrypt($date)
    Local $main_split = StringSplit($date, " ")
    If $main_split[0] - 2 Then
        Return SetError(1, 0, "") ; invalid time format
    EndIf
    Local $asDatePart = StringSplit($main_split[1], "/")
    Local $asTimePart = StringSplit($main_split[2], ":")
    If $asDatePart[0] - 3 Or $asTimePart[0] - 3 Then
        Return SetError(1, 0, "") ; invalid time format
    EndIf
    If $asDatePart[2] < 3 Then
        $asDatePart[2] += 12
        $asDatePart[1] -= 1
    EndIf
    Local $i_aFactor = Int($asDatePart[1] / 100)
    Local $i_bFactor = Int($i_aFactor / 4)
    Local $i_cFactor = 2 - $i_aFactor + $i_bFactor
    Local $i_eFactor = Int(1461 * ($asDatePart[1] + 4716) / 4)
    Local $i_fFactor = Int(153 * ($asDatePart[2] + 1) / 5)
    Local $aDaysDiff = $i_cFactor + $asDatePart[3] + $i_eFactor + $i_fFactor - 2442112
    Local $iTimeDiff = $asTimePart[1] * 3600 + $asTimePart[2] * 60 + $asTimePart[3]
    Return SetError(0, 0, $aDaysDiff * 86400 + $iTimeDiff)
EndFunc   ;==>_Epoch_encrypt
; from somewhere


Func URLEncode($urlText)
    $url = ""
    For $i = 1 To StringLen($urlText)
        $acode = Asc(StringMid($urlText, $i, 1))
        Select
            Case ($acode >= 48 And $acode <= 57) Or _
                    ($acode >= 65 And $acode <= 90) Or _
                    ($acode >= 97 And $acode <= 122)
                $url = $url & StringMid($urlText, $i, 1)
            Case $acode = 32
                $url = $url & "+"
            Case Else
                $url = $url & "%" & Hex($acode, 2)
        EndSelect
    Next
    Return $url
EndFunc   ;==>URLEncode
Func sha1($message)
    Return _Crypt_HashData($message, $CALG_SHA1)
EndFunc   ;==>sha1
Func md5($message)
    Return _Crypt_HashData($message, $CALG_MD5)
EndFunc   ;==>md5
Func hmac($key, $message, $hash = "md5")
    Local $blocksize = 64
    Local $a_opad[$blocksize], $a_ipad[$blocksize]
    Local Const $oconst = 0x5C, $iconst = 0x36
    Local $opad = Binary(''), $ipad = Binary('')
    $key = Binary($key)
    If BinaryLen($key) > $blocksize Then $key = Call($hash, $key)
    For $i = 1 To BinaryLen($key)
        $a_ipad[$i - 1] = Number(BinaryMid($key, $i, 1))
        $a_opad[$i - 1] = Number(BinaryMid($key, $i, 1))
    Next
    For $i = 0 To $blocksize - 1
        $a_opad[$i] = BitXOR($a_opad[$i], $oconst)
        $a_ipad[$i] = BitXOR($a_ipad[$i], $iconst)
    Next
    For $i = 0 To $blocksize - 1
        $ipad &= Binary('0x' & Hex($a_ipad[$i], 2))
        $opad &= Binary('0x' & Hex($a_opad[$i], 2))
    Next
    Return Call($hash, $opad & Call($hash, $ipad & Binary($message)))
EndFunc   ;==>hmac

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×