1

我正在使用 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);
                 }];    
}
4

1 回答 1

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));
}
于 2013-02-19T18:38:38.740 回答