2017-05-02 132 views
2

以下代码请求OAuth2令牌。它适用于Indy 10.0.52,但Indy 10 SVN 5412由于无效请求证书而生成400错误请求错误。Indy升级后的编码问题

WXYZUserID :=  Trim(GetSettingsValue(mySQLQuery, wcsWXYZUserID, '')); 
WXYZPassword :=  Trim(GetSettingsValue(mySQLQuery, wcsWXYZPassword, '')) 
WXYZSecret :=  Trim(GetSettingsValue(mySQLQuery, wcsWXYZSecret, '')); 
OAuthURL  :=  Trim(GetSettingsValue(mySQLQuery, wcsOAuthURL, '')); 
WxyzHttp := TIdHttp.Create(nil); 
Serial := TStringList.Create; 
if (WXYZUserID <> '') and (WXYZPassword <> '') and (WXYZSecret <> '') then 
    Serial.Add('grant_type=password&username=' + WXYZUserID + '&password=' + WXYZPassword) 
Else 
    Serial.Add('grant_type=password&username=****-*****&password=***************'); 

Output := TMemoryStream.Create; 
Serial.SaveToStream(Output); 

WxyzHttp.ConnectTimeout := 60000; 

IdLogFile := TIdLogFile.Create; 
IdLogFile.Filename := 'Logs/WxyzHTTP' + FOrmatDateTime('yyyymmdd_hhnnsszzz',now) + '.log'; 
IdLogFile.Active := True; 

IdSSLIOHandlerSocketOpenSSL1 := TIdSSLIOHandlerSocketOpenSSL.Create(nil); 
IdSSLIOHandlerSocketOpenSSL1.SSLOptions.Method := sslvSSLv23; 
IdSSLIOHandlerSocketOpenSSL1.Intercept := IdLogFile; 
WxyzHttp.IOHandler := IdSSLIOHandlerSocketOpenSSL1; 

if (WXYZUserID <> '') and (WXYZPassword <> '') and (WXYZSecret <> '') then 
    WxyzHttp.Request.CustomHeaders.Values['Authorization'] := 'Basic ' + WXYZSecret 
Else 
    WxyzHttp.Request.CustomHeaders.Values['Authorization'] := 'Basic ****************************************************************'; 

WxyzHttp.Request.ContentType := 'application/x-www-form-urlencoded; charset="utf-8"'; 

Try 
Token := WxyzHttp.Post(OAuthURL, Output); 
Except 
On E: Exception do 
Begin 
    DbgWebCatLog(wcmtDbg, 'GetSerialToken', E.ClassName + ' error raised, with message: ' + E.Message, ''); 
    Token := ''; 
    end; 
end; 

我添加了代码来捕获IdLogFile日期。 Indy 10.0.52版本的日志包含以下包含凭证的行。

Sent 05/01/2017 18:34:35: grant_type=password&username=*************_*****-*****&password=***}%25**(**(*** 

为了安全起见,我用'*'替换了所有的字母数字字符。 “%25”字符最初是实际密码中的“%”。有些东西将原始的“%”翻译成“%25”,但没有改变任何其他特殊字符。

对Indy 10 SVN 5412日志的初步检查检测到来自服务器的消息,指出存在无效的请求凭证。此外,与上面相对应的行中的所有特殊字符都显示了所有特殊字符已被编码。 grant_type,用户名和密码是使用我发现导致urlencoding的tStringList呈现的。我改变了代码以使用流呈现相同的数据,现在没有任何东西正在编码;甚至没有将“%”转换为“%25”,并且我继续收到无效请求凭证消息。

所以我的问题是为什么Indy 10.0.52只将“%”字符翻译为“%25”,以及如何在Indy 10 SVN 5412中复制该行为?

以下是Indy 10.0.52版本的完整日志。

Stat Connected. 
Sent 05/01/2017 18:34:35: POST /auth/realms/hvac/tokens/grants/access HTTP/1.0<EOL>Content-Type: application/x-www-form-urlencoded; charset="utf-8"<EOL>Content-Length: 82<EOL>Authorization: Basic<EOL> **********<EOL>Host: services.ccs.utc.com:443<EOL>Accept: text/html, */*<EOL>Accept-Encoding: identity<EOL>User-Agent: Mozilla/4.0 (compatible; MSIE 8.0)<EOL><EOL> 
Sent 05/01/2017 18:34:35: grant_type=password&username=*************_*****-*****&password=***}**%25**(**(*** 
Recv 05/01/2017 18:34:35: HTTP/1.1 200 OK<EOL>Server: Apache-Coyote/1.1<EOL>Pragma: no-cache<EOL>Cache-Control: no-store<EOL>Content-Type: application/json;charset=UTF-8<EOL>Content-Length: 569<EOL>Date: Mon, 01 May 2017 22:34:35 GMT<EOL>Connection: close<EOL><EOL>{<EOL> "access_token":"**********",<EOL> "token_type":"Bearer",<EOL> "expires_in":1800,<EOL> "refresh_token":"**********",<EOL> "id_token":"**********"<EOL>} 
Stat Disconnected. 
Stat Disconnected 

以下是从印第安纳波利斯10 SVN 5412版本,它失败,无效请求凭证完整的日志。

Stat Connected. 
Sent 05/02/2017 15:11:08: POST /auth/realms/hvac/tokens/grants/access HTTP/1.0<EOL>Connection: keep-alive<EOL>Content-Type: application/x-www-form-urlencoded; charset=utf-8<EOL>Content-Length: 82<EOL>Authorization: Basic **********<EOL>Host: services.ccs.utc.com<EOL>Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8<EOL>User-Agent: Mozilla/3.0 (compatible; Indy Library)<EOL><EOL> 
Sent 05/02/2017 15:11:08: grant_type=password&username=*************_*****-*****&password=***}**%**(**(***<EOL> 
Recv 05/02/2017 15:11:08: HTTP/1.1 400 Bad Request<EOL>Server: Apache-Coyote/1.1<EOL>Content-Type: application/json;charset=UTF-8<EOL>Content-Length: 84<EOL>Date: Tue, 02 May 2017 19:11:08 GMT<EOL>Connection: close<EOL><EOL>{<LF> "error": "invalid_grant",<LF> "error_description": "Invalid request credentials"<LF>} 
Stat Disconnected. 
Stat Disconnected. 

回答

4

您正在发布TMemoryStream,它被张贴原样TIdHTTP不编码以任何方式,在印第安纳波利斯的任何版本。您有责任确保您的TMemoryStream的内容格式正确。这是关于你的,而不是Indy。

TIdHTTP.Post()有一个过载,发布TStrings而不是TStream,格式为您的application/x-www-webform-urlencoded格式的字符串。根本不需要使用TMemoryStream

在印10.0.52,TIdHTTP.Post(TStrings)仅编码name=valuevalue对,并且它使用TIdURI.ParamsEncode(),其中百分之-编码是集合'*#%<> []'或不是#33..#128之间的ASCII字符,包括任何字符对它们进行编码。 10.0.52根本不支持Unicode,所以它不会将字符串编码为UTF-8,并且不支持Delphi 2009 +的UnicodeString类型(或者甚至WideString)。因此,您存储在TStrings中的任何字符串必须已经采用UTF-8格式(并且在Delphi 2009+的情况下,它们仍然必须采用UTF-8格式,而不是每个代码单元使用16位字符) 8位字符)。

在最新的Indy版本中(本文编写时为10.6.2.5418),TIdHTTP.Post(TStrings)完全支持Unicode,并且支持UnicodeString。它将编码既namevaluename=value双,并且通过在所述一组不编码任何Unicode字符手动对它们进行编码(不使用TIdURI)符合HTML5标准'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz*-._' 为UTF-8(默认情况下,可以使用本重写AByteEncoding参数Post())在百分比编码结果字节八位组之前。

随着中说,尝试更多的东西是这样的:

try 
    WXYZUserID :=  Trim(GetSettingsValue(mySQLQuery, wcsWXYZUserID, '')); 
    WXYZPassword :=  Trim(GetSettingsValue(mySQLQuery, wcsWXYZPassword, '')) 
    WXYZSecret :=  Trim(GetSettingsValue(mySQLQuery, wcsWXYZSecret, '')); 
    OAuthURL  :=  Trim(GetSettingsValue(mySQLQuery, wcsOAuthURL, '')); 

    WxyzHttp := TIdHttp.Create(nil); 
    try 
    WxyzHttp.ConnectTimeout := 60000; 

    IdLogFile := TIdLogFile.Create(WxyzHttp); 
    IdLogFile.Filename := 'Logs/WxyzHTTP' + FormatDateTime('yyyymmdd_hhnnsszzz',now) + '.log'; 
    IdLogFile.Active := True; 

    IdSSLIOHandlerSocketOpenSSL1 := TIdSSLIOHandlerSocketOpenSSL.Create(WxyzHttp); 
    IdSSLIOHandlerSocketOpenSSL1.SSLOptions.Method := sslvSSLv23; 
    IdSSLIOHandlerSocketOpenSSL1.Intercept := IdLogFile; 
    WxyzHttp.IOHandler := IdSSLIOHandlerSocketOpenSSL1; 

    if (WXYZSecret <> '') then 
     WxyzHttp.Request.CustomHeaders.Values['Authorization'] := 'Basic ' + WXYZSecret 
    else 
     WxyzHttp.Request.CustomHeaders.Values['Authorization'] := 'Basic ****************************************************************'; 

    WxyzHttp.Request.ContentType := 'application/x-www-form-urlencoded'; 
    WxyzHttp.Request.CharSet := 'utf-8'; 

    Serial := TStringList.Create; 
    try 
     Serial.Add('grant_type=password'); 

     if (WXYZUserID <> '') and (WXYZPassword <> '') then 
     begin 
     Serial.Add('username=' + WXYZUserID); 
     Serial.Add('password=' + WXYZPassword); 
     end else 
     begin 
     Serial.Add('username=****-*****'); 
     Serial.Add('password=***************'); 
     end; 

     Token := WxyzHttp.Post(OAuthURL, Serial); 
    finally 
     Serial.Free; 
    end; 
    finally 
    WxyzHttp.Free; 
    end; 
except 
    on E: Exception do 
    begin 
    DbgWebCatLog(wcmtDbg, 'GetSerialToken', E.ClassName + ' error raised, with message: ' + E.Message, ''); 
    Token := ''; 
    end; 
end; 
+0

谢谢雷米。重构构建tStringList的代码的确有窍门。 –