[twitter error 401]What should I do with the exclamation mark?

南笙酒味 提交于 2019-12-11 01:53:43

问题


Hi guys I'm facing a problem with Oauth and twitter api ver1.1. My app will tweet a post to twitter when post something to another site. But when the post body contains exclamation mark ('!'), I get twitter 401 error that is 'Unauthorized'. I searched on google and found that it may related to Oauth. Some people says that the '!' must be encoded to '%21'. I tried it but when i tweet '!' to twitter, I just got '%21' but not '!' itself. Anyone can give me some hint how can I encode the '!' correctly?


回答1:


you must encode your tweet with this category

#import "NSString+URLEncoding.h"
@implementation NSString (OAURLEncodingAdditions)

- (NSString *)URLEncodedString 
{
    NSString *result = (NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                                       (CFStringRef)self,
                                                                       NULL,
                                                                       CFSTR("!*'();:@&=+$,/?%#[]"),
                                                                       kCFStringEncodingUTF8);
    [result autorelease];
    return result;
}

- (NSString*)URLDecodedString
{
    NSString *result = (NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault,
                                                                                       (CFStringRef)self,
                                                                                       CFSTR(""),
                                                                                       kCFStringEncodingUTF8);
    [result autorelease];
    return result;  
}

@end


来源:https://stackoverflow.com/questions/17399066/twitter-error-401what-should-i-do-with-the-exclamation-mark

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!