I’m getting through every step of the OAuth process and get the access token. Go to post a tweet and get ‘Could not authenticate with OAuth’. I post the tweet right after getting the access token.
Double slashed lines are the comments. They are the results of the variable directly above them for analysis. Also note I’ve tried every type of combination every which way.
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,
ResposeStr : 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.
Signature := HMAC_SHA1_Sign(BaseString, SignKey);
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
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.
ResposeStr := fHTTP.Post(URI, EmptyParams);
JSON := SO(ResposeStr);
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;
Called like this:
SendRequest('POST', 'http://api.twitter.com/1/statuses/update.json', ['status=' + urlEncodeRFC3986(Text)]);