【问题标题】:Twitter API returns 401 in ColdFusionTwitter API 在 ColdFusion 中返回 401
【发布时间】:2013-12-19 20:42:46
【问题描述】:

我们正在尝试使用 ColdFusion 9 通过其 API v. 1.1 向 Twitter 发布更新,但尽管一切看起来应该可以正常工作,但我们仍会收到 401“未经授权”错误。任何帮助将不胜感激!

在这里回顾以前的帖子强化了我们所做的一切都是正确的信念,但也许有一些假设我们遗漏了。

以下三种方法都不适合我们(更新在表单字段中;更新在正文字段中;更新是 URL 的一部分。):

<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="vAA11oLz0R7kigH0iXwjQ", oauth_nonce="3D18D32A07048D54724AA0F304E83CFF", oauth_signature="TaEA6Ip9AJ07QKTxcWWT4fzPNpA%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1386088450", oauth_token="82684171-ptqIw8uOz0KNwBEL4AO7ue8DpzciWSNKvUqDoAf8p", oauth_version="1.0" ">
    <cfhttpparam type="header" name="Content-Type" value="application/x-www-form-urlencoded">
    <cfhttpparam type="formfield" name="status" value="HelloWorld" encoded="yes">
</cfhttp>



<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="vAA11oLz0R7kigH0iXwjQ", oauth_nonce="3D18D32A07048D54724AA0F304E83CFF", oauth_signature="TaEA6Ip9AJ07QKTxcWWT4fzPNpA%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1386088450", oauth_token="82684171-ptqIw8uOz0KNwBEL4AO7ue8DpzciWSNKvUqDoAf8p", oauth_version="1.0" ">
    <cfhttpparam type="header" name="Content-Type" value="application/x-www-form-urlencoded">
    <cfhttpparam type="body" value="status=HelloWorld" encoded="yes">
</cfhttp>



<cfhttp url="https://api.twitter.com/1.1/statuses/update.json?status=HelloWorld" method="POST" throwonerror="yes">
    <cfhttpparam type="header" name="Authorization" value="OAuth oauth_consumer_key="vAA11oLz0R7kigH0iXwjQ", oauth_nonce="3D18D32A07048D54724AA0F304E83CFF", oauth_signature="TaEA6Ip9AJ07QKTxcWWT4fzPNpA%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1386088450", oauth_token="82684171-ptqIw8uOz0KNwBEL4AO7ue8DpzciWSNKvUqDoAf8p", oauth_version="1.0" ">
    <cfhttpparam type="header" name="Content-Type" value="application/x-www-form-urlencoded">
</cfhttp>

Twitter 帐户已授予此应用的读/写访问权限。我们正在根据 https://dev.twitter.com/docs/auth/authorizing-requesthttps://dev.twitter.com/docs/auth/creating-signature 的指示进行编码,我们已经看到了这些 SO 页面:ColdFusion Twitter API Bad RequestColdFusion Post To Twitter Authentication ErrorTwitter, oauth and coldfusionTwitter API status update not working when updating to v1.1,我们尝试了 Apigee。

我们正在使用我们的访问令牌及其秘密来生成签名。我们在这里缺少什么?这应该是发布更新的单步过程,还是我们需要在尝试发布之前进行身份验证或授权的步骤? Twitter 开发网站上的 OAuth 工具选项卡会生成与我们的 ColdFusion 脚本生成的相同的标头等。

-肯

【问题讨论】:

  • monkehtweet 是肯的方式。下载并使用它...它会节省你拉头发的时间。同时你不应该在你的示例代码中发布实际的密钥和签名,除非你想邀请邪恶的黑客:)
  • 嗨,马克 - 密钥和令牌已重置,所以不用担心。我们确实尝试了monkeyhtweet,但发现更令人毛骨悚然,而且这条路线对于发布推文的单一目的来说似乎要简单得多。

标签: twitter coldfusion twitter-oauth coldfusion-9 http-status-code-401


【解决方案1】:

在 CF 中处理 OAuth 时,我总是使用这个库:http://oauth.riaforge.org/

这是我用来发送状态更新的一些脚本(它脱离了上下文,但应该给你一些想法):

<cfscript>
    //cfcs - from the OAuth library
    oauthSigMethodSHA = CreateObject("component", "OAuth.oauthsignaturemethod_hmac_sha1");
    oauthRequestCFC = CreateObject("component", "OAuth.oauthrequest");
    oauthConsumerCFC = CreateObject("component", "OAuth.oauthconsumer");
    oauthTokenCFC = CreateObject("component", "OAuth.oauthtoken");

    oTwitterConsumer = oauthConsumerCFC.init(sKey = MyConsumerKey, sSecret = MyConsumerSecret);

    ParamsStruct = StructNew();
    ParamsStruct['status'] = MyStatus;

    oTwitterAccessToken = oauthTokenCFC.init(sKey = MyAccessToken, sSecret = MyAccessSecret);

    //create request
    oTwitterReqest = oauthRequestCFC.fromConsumerAndToken(
        oConsumer   : oTwitterConsumer,
        oToken      : oTwitterAccessToken,
        sHttpMethod : "POST",
        sHttpURL    : 'https://api.twitter.com/1.1/statuses/update.json',
        stParameters: ParamsStruct
    );

    //sign request
    oTwitterReqest.signRequest(
        oSignatureMethod    : oauthSigMethodSHA,
        oConsumer           : oTwitterConsumer,
        oToken              : oTwitterAccessToken
    );
</cfscript>

<cfhttp method="POST" url="#oTwitterReqest.getString()#" result="result" throwonerror="no" charset="utf-8" redirect="no">
    <cfhttpparam type="header" name="status" value="#MyStatus#">
</cfhttp>

【讨论】:

  • 谢谢,卢卡斯,成功了!我们在monkeyhtweet 上投入了更多时间,让它也能正常工作,但(到目前为止)只有一个回调,我们不希望这个简单的单用户发布应用程序出现回调。也许这可以做我们想要的,但你发布的脚本很适合我们的需要,所以我们现在就这样做。 :-)
  • 酷,很高兴我能帮上忙。
猜你喜欢
  • 2017-04-04
  • 2016-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-05
  • 2010-11-03
  • 2019-11-05
  • 1970-01-01
相关资源
最近更新 更多