2013-02-18 96 views
1

我正在使用AFNetworking,AFOAuth1Client和AFLinkedInOAuth1Client從LinkedIn的API中獲取OAuth令牌。這一切都很好。針對LinkedIn API的OAuth不斷返回401,這是什麼原因造成的?

當我使用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

請張貼您的代碼。 – 2013-02-18 03:06:27

+2

我寫了AFLinkedInOAuth1Client子類(https://github.com/pj4533/AFLinkedInOAuth1Client),並且一直在調試。到目前爲止沒有運氣。我已經嘗試過的事情:1)刪除AFNetworking添加的默認標題2)使用http進行API調用,vs https進行宣誓3)嘗試了一個沒有'〜'的調用,認爲使用url編碼有點愚蠢。這些工作都沒有。 – pj4533 2013-02-19 18:03:31

+0

我試過1和2.然後我開始想,也許這是一個查詢字符串與頭部方法。 – 2013-02-19 20:55:58

回答

2

問題代碼是在AFPercentEscapedQueryStringPairMemberFromStringWithEncoding功能,內部AFOAuth1Client。它不需要跳過波浪號,它需要跳過逗號。

雖然這是一個靜態函數,但我不認爲我可以在AFLinkedInOAuth1Client中覆蓋它。我會跟進@mattt,看看他說什麼。現在你可以改變它到這個,讓它工作:

static NSString * AFPercentEscapedQueryStringPairMemberFromStringWithEncoding(NSString *string, NSStringEncoding encoding) { 
    static NSString * const kAFCharactersToBeEscaped = @":/?&=;[email protected]#$(),"; 
    static NSString * const kAFCharactersToLeaveUnescaped = @"[].~"; 
// static NSString * const kAFCharactersToBeEscaped = @":/?&=;[email protected]#$()~"; 
// static NSString * const kAFCharactersToLeaveUnescaped = @"[]."; 

    return (__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (__bridge CFStringRef)string, (__bridge CFStringRef)kAFCharactersToLeaveUnescaped, (__bridge CFStringRef)kAFCharactersToBeEscaped, CFStringConvertNSStringEncodingToEncoding(encoding)); 
} 
+0

fyi:https://github.com/AFNetworking/AFOAuth1Client/issues/24 – pj4533 2013-02-19 18:46:16

+0

你明星。感謝您查看這個。 – 2013-02-19 20:55:15

相關問題