2010-06-21 85 views
5

我正在嘗試發佈到twitter。我已經認證了應用程序,現在想要發佈更新。Twitter,oauth和coldfusion

這是我的HTTP POST是什麼:

<cfhttp url="http://api.twitter.com/1/statuses/update.json" method="post"> 
<cfhttpparam type="header" name="status" value="#urlEncodedFormat('my test post')#" /> 
<cfhttpparam type="header" name="oauth_consumer_key" value="xxx" /> 
<cfhttpparam type="header" name="oauth_nonce" value="xxx" /> 
<cfhttpparam type="header" name="oauth_signature_method" value="#urlEncodedFormat('HMAC-SHA1')#" /> 
<cfhttpparam type="header" name="oauth_token" value="xxx" /> 
<cfhttpparam type="header" name="oauth_timestamp" value="#GetTickCount()#" /> 
<cfhttpparam type="header" name="oauth_version" value="1.0" /> 
</cfhttp> 

有沒有人這樣做呢?我會走正確的路線嗎?

+1

的GetTickCount()返回以毫秒爲單位,api需要秒,所以不要忘記div 1000. – Henry 2010-06-21 02:46:15

+0

有人已經寫了一個CFC供Twitter使用。你看過那個嗎? http://twittercomponent.riaforge.org/ – ale 2010-06-21 18:12:01

+0

Andy不是提供解決方案,而是觀察:上面的代碼顯示了#urlEncodedFormat('HMAC-SHA1')#的使用,但這似乎表明存在誤解。該函數的arg是一個要格式化的URL,雖然它需要第二個參數來表示一個charest,但這不是一個有效的。希望可能的解決方案的其他指針有所幫助。你有沒有解決的事情? – 2011-11-14 16:39:43

回答

3

你看過這個嗎?

http://dev.twitter.com/pages/auth#auth-request

你需要構建 「簽名基本字符串」,後爲主體(警告:未經測試的代碼,CF8 +)

<cffunction name="makeSignatureBaseString" returntype="string" output="false"> 
    <cfargument name="httpMethod" type="string" required="true"> 
    <cfargument name="baseUri" type="string" required="true"> 
    <cfargument name="values" type="struct" required="true"> 

    <cfset var signatureBaseString = "#httpMethod#&#URLEncodedFormat(baseUri)#&"> 
    <cfset var keys = StructKeyArray(values)> 
    <cfset var key = ""> 

    <cfset ArraySort(keys, "textNoCase")> 
    <cfloop array="#keys#" index="key"> 
    <cfset signatureBaseString &= URLEncodedFormat("&#key#=#values[key]#")> 
    </cfloop> 

    <cfreturn signatureBaseString> 
</cffunction> 

-

<!--- using values from http://dev.twitter.com/pages/auth#auth-request ---> 
<cfset params = { 
    oauth_consumer_key = "GDdmIQH6jhtmLUypg82gる", 
    oauth_nonce = "oElnnMTQIZvqvlfXM56aBLAf5noGD0AQR3Fmi7Q6Y", 
    oauth_signature_method = "HMAC-SHA1", 
    oauth_token = "819797-Jxq8aYUDRmykzVKrgoLhXSq67TEa5ruc4GJC2rWimw", 
    oauth_timestamp = "1272325550", 
    oauth_version = "1.0" 
}> 

<cfhttp url="http://api.twitter.com/1/statuses/update.json" method="POST"> 
<cfloop collection="#params#" item="key"> 
    <cfheader type="header" name="#key#" value="#params[key]#"> 
</cfloop> 

<!--- add status to the params for makeSignatureBaseString() ---> 
<cfset params.status = "setting up my twitter 私のさえずりを設定する"> 

<cfhttpparam type="body" 
    value="#makeSignatureBaseString('POST', 'http://api.twitter.com/1/statuses/update.json', params)#"> 
</cfhttp>