我正在使用AFNetworking,AFOAuth1Client和AFLinkedInOAuth1Client从LinkedIn的API获取OAuth令牌。这一切都运作良好。
当我使用getPath拨打电话v1/people/~
时,我一直在收到401。
如果我将所有相同的值从我的代码推送到LinkedIn控制台,生成的链接将为我提供我所依赖的基本配置文件。
造成401的原因是什么?我感觉它是AFNetworking或我的配置。
另外,您对如何诊断潜在问题有什么建议吗?
以下代码
+ (JJLinkedInClient *)sharedInstance {
DEFINE_SHARED_INSTANCE_USING_BLOCK(^{
return [[self alloc] init];
});
}
- (id)init {
if ( (self = [super init]) ) {
_client = [[AFLinkedInOAuth1Client alloc] initWithBaseURL:[NSURL URLWithString:kJJLinkedInAPIBaseURLString]
key:@"XXXXXXXX"
secret:@"YYYYYYYY"];
// [_client registerHTTPOperationClass:[AFJSONRequestOperation class]];
[_client registerHTTPOperationClass:[AFHTTPRequestOperation class]];
// [_client setDefaultHeader:@"Accept" value:@"application/json"];
}
return self;
}
- (void)authorize:(void(^)())success {
__block JJLinkedInClient *weakSelf = self;
[self.client authorizeUsingOAuthWithRequestTokenPath:@"uas/oauth/requestToken"
userAuthorizationPath:@"uas/oauth/authorize"
callbackURL:[NSURL URLWithString:@"XXXXXXX://linkedin-auth-success"]
accessTokenPath:@"uas/oauth/accessToken"
accessMethod:@"POST"
success:^(AFOAuth1Token *accessToken) {
NSLog(@"Success: %@", accessToken);
[weakSelf getProfile];
success();
} failure:^(NSError *error) {
NSLog(@"Error: %@", error);
}];
}
- (void)getProfile {
[self.client getPath:@"v1/people/~"
parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"%@", responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@", error);
}];
}
答案 0 :(得分:2)
问题出在AFOAuth1Client内的AFPercentEscapedQueryStringPairMemberFromStringWithEncoding函数中。它不需要逃脱波浪号,它需要逃避逗号。
因为这是一个静态函数,我不认为我可以在AFLinkedInOAuth1Client中覆盖它。我将跟进@mattt并看看他说的话。现在您可以将其更改为此,以使其正常工作:
static NSString * AFPercentEscapedQueryStringPairMemberFromStringWithEncoding(NSString *string, NSStringEncoding encoding) {
static NSString * const kAFCharactersToBeEscaped = @":/?&=;+!@#$(),";
static NSString * const kAFCharactersToLeaveUnescaped = @"[].~";
// static NSString * const kAFCharactersToBeEscaped = @":/?&=;+!@#$()~";
// static NSString * const kAFCharactersToLeaveUnescaped = @"[].";
return (__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (__bridge CFStringRef)string, (__bridge CFStringRef)kAFCharactersToLeaveUnescaped, (__bridge CFStringRef)kAFCharactersToBeEscaped, CFStringConvertNSStringEncodingToEncoding(encoding));
}