2013-01-05 7 views
5

का उपयोग करने में असमर्थ IOS 6 पर AFOAuth2Client और AFNetworking का उपयोग करके मैं एक्सेस टोकन प्राप्त करने में सक्षम हूं, लेकिन संसाधन तक पहुंचने में असमर्थ हूं, सर्वर 401 अनधिकृत स्थिति कोड के साथ प्रतिक्रिया देता है। यह ओओथ प्रदाता के रूप में डोरकीपर का उपयोग कर एक कस्टम रेल 3 एपीआई बैकएंड के खिलाफ है। निम्न क्लाइंट गहरे लाल रंग का कोड, OAuth2 मणि ​​का उपयोग कर लें, तो ठीक काम करता है:AFOAuth2Client संसाधन

client = OAuth2::Client.new(app_id, secret, site: "http://subdomain.example.com/") 
access_token = client.password.get_token('username', 'password') 
access_token.get('/api/1/products').parsed 

आईओएस कोड के रूप में नीचे है, प्रवेश बटन हैंडलर में मैं उपयोगकर्ता नाम और पासवर्ड का उपयोग करके प्रमाणित, और साख की दुकान:

- (IBAction)login:(id)sender { 
    NSString *username = [usernameField text]; 
    NSString *password = [passwordField text]; 

    NSURL *url = [NSURL URLWithString:kClientBaseURL]; 
    AFOAuth2Client *client = [AFOAuth2Client clientWithBaseURL:url clientID:kClientID secret:kClientSecret]; 

    [client authenticateUsingOAuthWithPath:@"oauth/token" 
           username:username 
           password:password 
           scope:nil 
           success:^(AFOAuthCredential *credential) { 
            NSLog(@"Successfully received OAuth credentials %@", credential.accessToken); 
            [AFOAuthCredential storeCredential:credential 
                 withIdentifier:client.serviceProviderIdentifier]; 
            [self performSegueWithIdentifier:@"LoginSegue" sender:sender]; 
           } 
           failure:^(NSError *error) { 
            NSLog(@"Error: %@", error); 
            [passwordField setText:@""]; 
           }]; 
} 

और मैं अपने अंत बिंदु के लिए AFHTTPClient subclassed है और initWithBaseURL में यह साख प्राप्त करता है और पहुँच टोकन के साथ प्राधिकरण हैडर सेट:

- (id)initWithBaseURL:(NSURL *)url { 
    self = [super initWithBaseURL:url]; 
    if (!self) { 
     return nil; 
    } 

    [self registerHTTPOperationClass:[AFJSONRequestOperation class]]; 
    [self setDefaultHeader:@"Accept" value:@"application/json"]; 

    AFOAuthCredential *credential = [AFOAuthCredential retrieveCredentialWithIdentifier:@"subdomain.example.com"]; 
    [self setAuthorizationHeaderWithToken:credential.accessToken]; 

    return self; 
} 

क्या यह AFOAuth2Client और AFNetworking का उपयोग करने का सही तरीका है? और कोई विचार क्यों यह काम नहीं कर रहा है?

उत्तर

5

बदलकर इस काम कर पाने में कामयाब रहे:

AFOAuthCredential *credential = [AFOAuthCredential retrieveCredentialWithIdentifier:@"subdomain.example.com"]; 
    [self setAuthorizationHeaderWithToken:credential.accessToken]; 

रहे हैं:

AFOAuthCredential *credential = [AFOAuthCredential retrieveCredentialWithIdentifier:@"subdomain.example.com"]; 
    NSString *authValue = [NSString stringWithFormat:@"Bearer %@", credential.accessToken]; 
    [self setDefaultHeader:@"Authorization" value:authValue]; 

अद्यतन

क्या मैं था कि AFOAuth2Client ही AFHTTPClient का एक उपवर्ग तो कर सकते हैं नोटिस में असफल रहे थे एपीआई कक्षा के बेस क्लास के रूप में इस्तेमाल किया जाए, उदाहरण के लिए:

@interface YFExampleAPIClient : AFOAuth2Client 

    + (YFExampleAPIClient *)sharedClient; 

    /** 

    */ 
    - (void)authenticateWithUsernameAndPassword:(NSString *)username 
             password:(NSString *)password 
             success:(void (^)(AFOAuthCredential *credential))success 
             failure:(void (^)(NSError *error))failure; 

    @end 

और कार्यान्वयन हो जाता है:

@implementation YFExampleAPIClient 

+ (YFExampleAPIClient *)sharedClient { 
    static YFExampleAPIClient *_sharedClient = nil; 
    static dispatch_once_t onceToken; 
    dispatch_once(&onceToken, ^{ 
     NSURL *url = [NSURL URLWithString:kClientBaseURL]; 
     _sharedClient = [YFExampleAPIClient clientWithBaseURL:url clientID:kClientID secret:kClientSecret]; 
    }); 

    return _sharedClient; 
} 

- (void)authenticateWithUsernameAndPassword:(NSString *)username 
            password:(NSString *)password 
            success:(void (^)(AFOAuthCredential *credential))success 
            failure:(void (^)(NSError *error))failure { 
    [self authenticateUsingOAuthWithPath:@"oauth/token" 
            username:username 
            password:password 
            scope:nil 
            success:^(AFOAuthCredential *credential) { 
             NSLog(@"Successfully received OAuth credentials %@", credential.accessToken); 
             [self setAuthorizationHeaderWithCredential:credential]; 
             success(credential); 
            } 
            failure:^(NSError *error) { 
             NSLog(@"Error: %@", error); 
             failure(error); 
            }]; 
} 

- (id)initWithBaseURL:(NSURL *)url 
      clientID:(NSString *)clientID 
       secret:(NSString *)secret { 
    self = [super initWithBaseURL:url clientID:clientID secret:secret]; 
    if (!self) { 
     return nil; 
    } 

    [self setDefaultHeader:@"Accept" value:@"application/json"]; 

    return self; 
} 

@end 

ध्यान दें कि initWithBaseURL HTTP हेडर को स्वीकार स्थापित करने के लिए ओवरराइड की गई है।

पूर्ण स्रोत कोड GitHub पर उपलब्ध है - https://github.com/yellowfeather/rails-saas-ios

+1

https://github.com/AFNetworking/AFOAuth2Client/blob/master/AFOAuth2Client/AFOAuth2Client.h फ़ाइल पर एक नजर डालें। शीर्ष पर एक नोट है कि यह अनुशंसा की जाती है कि आप इसे अपनी बेस क्लास के रूप में उपयोग न करें। – davidbitton