问题
First off, my project requires me to make my own module so I can't use the various packages and products out there. Having said that, using the correct token, consumer_key, consumer_secret and token_secret variables, creating the correct Base Signature String and thus OAuth Signature from that string, I have had NO problem grabbing Twitter data via my ColdFusion module. If either of them were off, I wouldn't be able to even get Twitter data.
So knowing that my variables are correct, I am still unable to POST a simple status tweet to my Twitter account via ColdFusion. Each time I try I get the same error: "Code:32, Message:'Could not authenticate you'". Since I know it can't be the variables, it has got to be how I'm sending the POST via ColdFusion, but I am unsure of what I am doing wrong. The following is the code I use to submit the POST:
<cfhttp url="https://api.twitter.com/1.1/statuses/update.json" method="POST" throwonerror="yes" >
<cfhttpparam type="header" name="Authorization" value='OAuth oauth_consumer_key="#oauthStruct.oauth_consumer_key#", oauth_nonce="#oauthStruct.oauth_nonce#", oauth_signature="#oauthStruct.oauth_signature#", oauth_signature_method="#oauthStruct.oauth_signature_method#", oauth_timestamp="#oauthStruct.oauth_timestamp#", oauth_token="#oauthStruct.oauth_token#", oauth_version="#oauthStruct.oauth_version#"'>
<cfhttpparam type="header" name="Content-Type" value="application/x-www-form-urlencoded">
<cfhttpparam type="formfield" name="status" value="#oauthStruct.status#">
</cfhttp>
Can somebody please look over the above code and help me figure out if I'm missing something or submitting incorrectly this call?
Update: I changed the above code to this:
<cfhttp url="https://api.twitter.com/1.1/statuses/update.json" method="POST" throwonerror="yes" >
<cfhttpparam type="header" name="Authorization" value='OAuth oauth_consumer_key="#oauthStruct.oauth_consumer_key#", oauth_nonce="#oauthStruct.oauth_nonce#", oauth_signature="#oauthStruct.oauth_signature#", oauth_signature_method="#oauthStruct.oauth_signature_method#", oauth_timestamp="#oauthStruct.oauth_timestamp#", oauth_token="#oauthStruct.oauth_token#", oauth_version="#oauthStruct.oauth_version#"'>
<cfhttpparam type="header" name="Content-Type" value="application/x-www-form-urlencoded">
<cfhttpparam type="header" name="oauth_consumer_key" value="#oauthStruct.oauth_consumer_key#">
<cfhttpparam type="header" name="oauth_nonce" value="#oauthStruct.oauth_nonce#">
<cfhttpparam type="header" name="oauth_signature" value="#oauthStruct.oauth_signature#">
<cfhttpparam type="header" name="oauth_signature_method" value="#oauthStruct.oauth_signature_method#">
<cfhttpparam type="header" name="oauth_timestamp" value="#oauthStruct.oauth_timestamp#">
<cfhttpparam type="header" name="oauth_token" value="#oauthStruct.oauth_token#">
<cfhttpparam type="header" name="oauth_version" value="#oauthStruct.oauth_version#">
<cfhttpparam type="body" value="#signature_string#">
</cfhttp>
It still does not work, same authenticate error. Any other ideas?
回答1:
I figured it out. So that others can understand how this process goes, with the new API (1.1), this is how you post a status update to your Twitter account:
<cfhttp url="https://api.twitter.com/1.1/statuses/update.json" method="POST" throwonerror="yes" >
<cfhttpparam type="header" name="Authorization" value='OAuth oauth_consumer_key="#oauthStruct.oauth_consumer_key#", oauth_nonce="#oauthStruct.oauth_nonce#", oauth_signature="#oauthStruct.oauth_signature#", oauth_signature_method="#oauthStruct.oauth_signature_method#", oauth_timestamp="#oauthStruct.oauth_timestamp#", oauth_token="#oauthStruct.oauth_token#", oauth_version="#oauthStruct.oauth_version#"'>
<cfhttpparam type="header" name="Content-Type" value="application/x-www-form-urlencoded">
<cfhttpparam type="body" value="status=#oauthStruct.status#">
</cfhttp>
If I did not have the Authorization header parameter, I ended up with a 400 Bad Request error. You put all the necessary OAuth parameters, including your digital signature, in a comma delimited list as shown above, indicate your content-type, and most importantly, you provide as a URL Query String the remaining variables in the body parameter of your cfhttp call.
来源:https://stackoverflow.com/questions/13404906/coldfusion-post-to-twitter-authentication-error