使用 URL 编码字符串时,iPhone 上的 OADataFetcher 出现问题

发布于 2024-08-23 10:47:43 字数 1514 浏览 5 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜柠檬 2024-08-30 10:47:43

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.

眼前雾蒙蒙 2024-08-30 10:47:43

问题在于标准 OAuth 库使用有缺陷的 stringByAddingPercentEscapesUsingEncoding 对用户名和密码进行编码。

我的解决方案是创建一个类别来覆盖 OARequestParameter 中的以下方法

 - (NSString *)URLEncodedName 
{
 return [self.name stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
}

- (NSString *)URLEncodedValue 
{
    return [self.value stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
}

,即:

将以下内容放在项目中的某个位置:

//the .h file
#import "OARequestParameter.h"

@interface OARequestParameter (YourApp)

@end

并使用以下方法覆盖有问题的方法:

#import "OARequestParameter+YourApp.h"


@implementation OARequestParameter (YourApp)

-(NSString*)encode:(NSString*)unencodedString
{
 NSString * encodedString = (NSString *)CFURLCreateStringByAddingPercentEscapes(
                       NULL,
                       (CFStringRef)unencodedString,
                       NULL,
                       (CFStringRef)@"!*'();:@&=+$,/?%#[]",
                       kCFStringEncodingUTF8 );
 return [encodedString autorelease]; 
}

- (NSString *)URLEncodedName 
{
 //return [self.name stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
 return [self encode:self.name];
}

- (NSString *)URLEncodedValue 
{
    //return [self.value stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
 return [self encode:self.value];
}

@end

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

 - (NSString *)URLEncodedName 
{
 return [self.name stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
}

- (NSString *)URLEncodedValue 
{
    return [self.value stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
}

i.e:

Put the following somewhere in your project:

//the .h file
#import "OARequestParameter.h"

@interface OARequestParameter (YourApp)

@end

and override the buggy methods with:

#import "OARequestParameter+YourApp.h"


@implementation OARequestParameter (YourApp)

-(NSString*)encode:(NSString*)unencodedString
{
 NSString * encodedString = (NSString *)CFURLCreateStringByAddingPercentEscapes(
                       NULL,
                       (CFStringRef)unencodedString,
                       NULL,
                       (CFStringRef)@"!*'();:@&=+$,/?%#[]",
                       kCFStringEncodingUTF8 );
 return [encodedString autorelease]; 
}

- (NSString *)URLEncodedName 
{
 //return [self.name stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
 return [self encode:self.name];
}

- (NSString *)URLEncodedValue 
{
    //return [self.value stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
 return [self encode:self.value];
}

@end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文