我在使用Overbyte的ICS套件从Instapaper api获得访问令牌时遇到了麻烦。奇怪的是,我能够使用Indy获得访问令牌。我使用的是Delphi XE2和ICS version 7。
出于调试目的,我使用固定字符串作为post数据和授权头。由于Indy代码可以工作,所以问题似乎与签名无关。
我想让这个工作与ICS…对于代码为什么不能工作有什么想法吗?我得到一个无效的签名错误(签名与期望值不匹配)。授权头和post body应该与我用Indy发送的内容相同,所以听起来像是与ICS请求有关。
这是Indy代码的工作(我得到oauth_token和oauth_token_secret返回)
var
Params: TStringList;
AuthHeader: STring;
idHTTP: TIDHTTP;
SSLIOHandler: TIdSSLIOHandlerSocketOpenSSL;
begin
idHTTP := TIDHTTP.Create(Self);
SslIOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(Self);
idHTTP.IOHandler := SSLIOHandler;
idHTTP.HTTPOptions := [];
Params := TStringList.Create;
Params.Add('x_auth_mode=client_auth');
Params.Add('x_auth_password=mypassword');
Params.Add('x_auth_username=myusername%40gmail.com');
idHTTP.Request.Host := 'https://www.instapaper.com';
AuthHeader := 'Authorization: ' + 'OAuth oauth_consumer_key="myconsumerkey", oauth_nonce="BAFWMTGFXNXSTNKUENBC", oauth_signature_method="HMAC-SHA1", oauth_signature="sEfq8gHlHYfrmLy5u0kKBgTYmVA%3D", oauth_timestamp="1369669197", oauth_version="1.0"';
idHTTP.request.CustomHeaders.Add(AuthHeader);
Response := idHTTP.Post('https://www.instapaper.com/api/1/oauth/access_token', Params);
end;
以下是ICS代码失败(无效签名):
var
PostData: AnsiString;
begin
PostData := 'x_auth_mode=client_auth&x_auth_password=mypassword&x_auth_username=myusername%40gmail.com';
SSLHTTPClient := TSslHttpCli.Create(nil);
With SSLHTTPClient do
begin
RcvdStream := TMemoryStream.Create;
SendStream := TMemoryStream.Create;
SendStream.Seek(0, soFromBeginning);
SendStream.Write(PostData[1], Length(PostData));
SendStream.Seek(0, soFromBeginning);
URL := 'https://www.instapaper.com/api/1/oauth/access_token';
OnRequestDone := SSLHTTPClientRequestDone;
OnBeforeHeaderSend := HTTPClientBeforeHeaderSend; // add the authorization header
SslContext := TSslContext.Create(SSLHTTPClient);
PostAsync;
end;
end;
procedure TInstapaperOAuth.HTTPClientBeforeHeaderSend(Sender: TObject; const Method: string; Headers: TStrings);
var
Header: String;
begin
Header := 'Authorization: ' + 'OAuth oauth_consumer_key="myconsumerkey", oauth_nonce="BAFWMTGFXNXSTNKUENBC", oauth_signature_method="HMAC-SHA1", oauth_signature="sEfq8gHlHYfrmLy5u0kKBgTYmVA%3D", oauth_timestamp="1369669197", oauth_version="1.0"';
Headers.Add(Header);
end;
我接受上面的评论作为这个问题的答案。所以要解决这个问题,更新HTTPClientBeforeHeaderSend来替换主机记录,并添加oauth头:
procedure TInstapaperOAuth.HTTPClientBeforeHeaderSend(Sender: TObject; const Method: string; Headers: TStrings);
var
Header: String;
I: Integer;
begin
Header := 'Authorization: ' + 'OAuth oauth_consumer_key="myconsumerkey", oauth_nonce="BAFWMTGFXNXSTNKUENBC", oauth_signature_method="HMAC-SHA1", oauth_signature="sEfq8gHlHYfrmLy5u0kKBgTYmVA%3D", oauth_timestamp="1369669197", oauth_version="1.0"';
Headers.Add(Header);
//=======================================
// find the host record and replace it
//=======================================
for I := 0 to Headers.Count - 1 do
begin
if pos('Host:',Headers.Strings[I]) > 0 then
begin
Headers.Strings[I] := 'Host: www.instapaper.com';
break;
end;
end;
end;