所以,我在Delphi中做一个简单的推特应用程序,因为我找不到一个有效的库,所以我决定创建自己的库。通过身份验证过程,我遵循一切,并获得Access令牌和秘密令牌就好了。
当我尝试使用它们时(在下面的代码中)我收到错误{“error”:“无法通过OAuth进行身份验证。”}
对于读取和写入请求(验证和发布)都是如此。任何帮助都会非常感激,因为我一直在靠着墙撞击我的头。
以下代码中的命令是注释正上方的变量内容。
编辑:即使只是知道可能导致“无法通过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;
答案 0 :(得分:1)
所以我发现了这一行:
fHTTP.Request.CustomHeaders.AddValue('Authorization', AuthHeader);
组件处理自定义标头的方式是文本的分隔符是逗号。这意味着程序在为OAuth包含的每个参数之后添加\ r \ n(新行)。难怪它失败了! Twitter甚至没有收到参数!
由于我在将标题添加到标题之前检查标题,我甚至不认为这是问题所在。
感谢您的帮助!我可能不会深入研究这个代码。
答案 1 :(得分:0)
我创建了一个简单的库/类来发送状态更新(发布消息):