使用 URL 编码字符串时,iPhone 上的 OADataFetcher 出现问题
我在 iPhone twitter 应用程序中使用 OAuth/xAuth,并且遇到包含特殊字符的密码问题。例如“密码&”。首先,我正在转换 &通过执行以下操作将其编码为 %26:
NSString *encodedPassword = (NSString *)CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef)thePassword,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8 );
这将其很好地编码为“password%26”。然后,我将其设置为 OAMutableURLRequest 的参数:
[request setParameters:[NSArray arrayWithObjects:
[OARequestParameter requestParameterWithName:@"x_auth_mode" value:@"client_auth"],
[OARequestParameter requestParameterWithName:@"x_auth_username" value:encodedUsername],
[OARequestParameter requestParameterWithName:@"x_auth_password" value:encodedPassword],
nil]];
使用 OADataFetcher 发送请求后,我收到 NSUrlError:
错误域=NSURLErrorDomain代码=-1012 UserInfo=0x4823e00“操作无法完成。(NSURLErrorDomain错误-1012。)”
我在这里做错了什么?如果我离开 &在密码中它使我的代码崩溃(setParamaters 数组失败)。有人在使用 OAuth 之前遇到过这个吗?
如果我有字母数字密码,一切正常...感谢您的指导!
I'm using OAuth/xAuth in an iPhone twitter app, and having a problem with passwords that contain special characters. For example "password&". First off, I am converting the & into %26 by doing:
NSString *encodedPassword = (NSString *)CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef)thePassword,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8 );
This encodes it fine into "password%26". I am then setting it up as a parameter of an OAMutableURLRequest:
[request setParameters:[NSArray arrayWithObjects:
[OARequestParameter requestParameterWithName:@"x_auth_mode" value:@"client_auth"],
[OARequestParameter requestParameterWithName:@"x_auth_username" value:encodedUsername],
[OARequestParameter requestParameterWithName:@"x_auth_password" value:encodedPassword],
nil]];
Upon sending the request using an OADataFetcher, I receive an NSUrlError:
Error Domain=NSURLErrorDomain Code=-1012 UserInfo=0x4823e00 "Operation could not be completed. (NSURLErrorDomain error -1012.)"
What am I doing wrong here? If I leave the & in the password it crashes my code (the setParamaters array flunks out). Has anyone come across this before using OAuth?
If I have a alphanumeric password everything works fine... Thanks for any guidance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
OAuthRequestParameter 已经对名称和值进行了编码;可能是 &特别是引起问题。您是否尝试过使用不同的非标准字符?它可能需要修改 OAuth 库本身。
OAuthRequestParameter already encodes the name and value; it may be that the & in particular is causing problems. Have you tried using different non-standard characters? It might required modifying the OAuth library itself.
问题在于标准 OAuth 库使用有缺陷的 stringByAddingPercentEscapesUsingEncoding 对用户名和密码进行编码。
我的解决方案是创建一个类别来覆盖 OARequestParameter 中的以下方法
,即:
将以下内容放在项目中的某个位置:
并使用以下方法覆盖有问题的方法:
The problem is that the standard OAuth libary using the buggy stringByAddingPercentEscapesUsingEncoding to encode the username and password.
My solution was to make a category to override the following methods in OARequestParameter
i.e:
Put the following somewhere in your project:
and override the buggy methods with: