【问题标题】:Twitter - Could not authenticate with OAuth (401) ErrorTwitter - 无法使用 OAuth 进行身份验证 (401) 错误
【发布时间】:2013-01-11 11:06:30
【问题描述】:

所以,我在 Delphi 中做一个简单的 twitter 应用程序,因为我找不到一个已经可以工作的库,所以我决定创建自己的。通过身份验证过程,我遵循一切并获得访问令牌和秘密令牌就好了。

当我尝试使用它们时(在以下代码中),我收到错误 {"error":"Could not authenticate with OAuth."}

这对于读取和写入请求(验证和发布)都是如此。任何帮助都将不胜感激,因为我一直在为此撞墙。

下面代码中的命令是直接在 cmets 上面的变量的内容。

编辑:即使只是知道什么可能导致“无法使用 OAuth 进行身份验证”而不是更具体的错误(“无效签名”、“缺少参数”)将是一个巨大的帮助!

编辑 2:Wireshark 数据包结果。

HTTP/1.1 401 Unauthorized
Date: Tue, 06 Sep 2011 20:11:13 GMT
Server: hi
Status: 401 Unauthorized
WWW-Authenticate: OAuth realm="http://api.twitter.com"
X-Runtime: 0.01306
Content-Type: application/json; charset=utf-8
Content-Length: 114
Cache-Control: no-cache, max-age=300
Set-Cookie: _twitter_sess=xxxxxxxxxxx; domain=.twitter.com; path=/; HttpOnly
Expires: Tue, 06 Sep 2011 20:16:13 GMT
Vary: Accept-Encoding
Connection: close
{"error":"Could not authenticate with OAuth.","request":"\/1\/statuses\/update.json?status=This%20is%20a%20test."}

代码:

function TTwitter.SendRequest(Method: String; URI: String; Params: Array of String): ISuperObject;
const
  AuthHeaderFormat = 'OAuth oauth_nonce="%s", oauth_signature_method="%s", oauth_timestamp="%s", oauth_consumer_key="%s", oauth_token="%s", oauth_signature="%s", oauth_version="%s"';
var
  oauth_nonce,
  oauth_timestamp,
  SignKey,
  BaseString,
  Signature,
  AuthHeader,
  ResponseStr : String;
  BaseStringParams : Array of String;
  JSON : ISuperObject;
  I, J : Integer;
  EmptyParams : TStringList;
begin
  oauth_nonce := GenerateNonce;
  oauth_timestamp := GenerateTimeStamp;
  SignKey := fConsumerSecret + '&' + fAccessToken;
  J := 0;
  SetLength(BaseStringParams, Length(Params) + 6);
  For I := Low(Params) To High(Params) Do
    begin
    BaseStringParams[J] := Params[I];
    Inc(J);
  end;
  BaseStringParams[J] := 'oauth_consumer_key=' + fConsumerKey;
  BaseStringParams[J + 1] := 'oauth_nonce=' + oauth_nonce;
  BaseStringParams[J + 2] := 'oauth_signature_method=' + oauth_signature_method;
  BaseStringParams[J + 3] := 'oauth_token=' + fOAuthToken;
  BaseStringParams[J + 4] := 'oauth_timestamp=' + oauth_timestamp;
  BaseStringParams[J + 5] :=  'oauth_version=' + oauth_version;
  BaseString := CreateBaseString('POST', URI, BaseStringParams);
  //POST&http%3A%2F%2Fapi.twitter.com%2F1%2Fstatuses%2Fupdate.json&oauth_consumer_key%3DXXXXXXXXXXXXXXXX%26oauth_nonce%3D0F95A680F2231F689C702FCECAD1D449%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1314988258%26oauth_token%3DXXXXXX-XXXXXXXXXXXXXXXX%26oauth_version%3D1.0%26status%3DThis%2520is%2520a%2520test.
  AuthHeader := Format(AuthHeaderFormat, [oauth_nonce,
                                          oauth_signature_method,
                                          oauth_timestamp,
                                          fConsumerkey,
                                          fOAuthToken,
                                          Signature,
                                          oauth_version]);
  //OAuth oauth_nonce="0F95A680F2231F689C702FCECAD1D449", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1314988258", oauth_consumer_key="XXXXXXXXXXXXXXXXXX", oauth_token="XXXXXX-XXXXXXXXXXXXXXXX", oauth_signature="XXXXXXXXXXXXXXXXXXXXX", oauth_version="1.0"
  fHTTP.Request.CustomHeaders.AddValue('Authorization', AuthHeader);
  EmptyParams := TStringList.Create;
  Try
    If Length(Params) > 0 Then
      begin
      URI := URI + '?';
      For I := Low(Params) To High(Params) Do
        begin
        URI := URI + Params[I] + '&';
      end;
      URI := Copy(URI, 1, Length(URI) - 1);
      //http://api.twitter.com/1/statuses/update.json?status=This%20is%20a%20test.
    end;
    If Method = 'POST' Then
      begin
      ResponseStr := fHTTP.Post(URI, EmptyParams);
    end
    Else If Method = 'GET' Then
      begin
      ResponseStr := fHTTP.Get(URI);
    end;
    JSON := SO(ResponseStr);
    Result := JSON;
  Except
    On E:EIdHTTPProtocolException Do
      begin
      MessageBox(0, PChar(E.ErrorMessage), '', 0);
      //{"error":"Could not authenticate with OAuth.","request":"\/1\/statuses\/update.json?status=This%20is%20a%20test."}
    end;
  end;
  EmptyParams.Free;
end;

【问题讨论】:

  • 首先,检查您机器上的日期/时间设置。那么,如何生成签名呢?我在您的代码中没有看到这一点。你确定它是正确的吗?第三 - 您是否尝试过解析响应的标头(或使用 Wireshark 嗅探它们)?其中可能有错误的详细信息。
  • 日期和时间设置正确,与签名相同(尽管此特定签名中需要包含什么内容)。如果其中任何一个不正确,则使用相同事物的身份验证步骤也会失败。此外,这些问题还有特定的错误。我查看了原始标题,其中包含的唯一错误是我列出的错误。这就是为什么我这么难过。
  • 返回详细信息。我尝试使用无效令牌进行授权。这是我得到的响应(在 Java 中有 getErrorStream,即使服务器回复非 200,它也会返回任何有用的数据): {"error":"Invalid \/ expired Token","re​​quest":"\/1\/帐户\/verify_credentials.json"}。尝试获取该数据。我认为最简单的方法是使用 Wireshark。
  • 那我不知道。尝试使用其他工具或库运行相同的请求,嗅探 HTTP 包并将其与您的进行比较。例如,有 Twitter 的 twurl (dev.twitter.com/console) 或者很多 Java、C#、Ruby、Python 等的库。

标签: delphi twitter oauth


【解决方案1】:

所以我在这一行发现了:

fHTTP.Request.CustomHeaders.AddValue('Authorization', AuthHeader);

组件处理自定义标题的方式是文本的分隔符是逗号。这意味着程序在我为 OAuth 包含的每个参数之后添加了一个 \r\n(新行)。难怪失败了! Twitter 甚至没有收到参数!

因为我在将标题添加到标题之前检查了标题,所以我什至不认为这是问题所在。

感谢您的帮助!否则我可能不会深入研究代码。

【讨论】:

    【解决方案2】:

    我制作了一个简单的库/类来发送状态更新(发布消息):

    http://code.google.com/p/delphi-twitter-library/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-04
      • 2014-05-01
      • 1970-01-01
      • 2013-02-15
      • 1970-01-01
      • 2017-07-11
      • 2019-05-06
      相关资源
      最近更新 更多