iOS'ta AFOAuth2Client ve AFNetworking'i kullanma 6 Erişim belirteci alabiliyorum, ancak bir kaynağa erişemiyorum, sunucu 401 yetkisiz durum koduyla yanıt veriyor. Bu, OAuth sağlayıcısı olarak görev yapan bir özel Rails 3 API arka ucuna aykırıdır. aşağıdaki istemci yakut kodu, OAuth2 gem kullanarak, Tamam çalışır:AFOAuth2Client kaynağına erişemiyor
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
iOS kod giriş düğmesi işleyicisinde ben kullanıcı adı ve şifreyi kullanarak kimlik doğrulaması ve kimlik bilgilerini depolamak, aşağıdaki gibidir:
- (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:@""];
}];
}
ve benim son nokta için AFHTTPClient
sınıflandırma ettik ve initWithBaseURL
o kimlik bilgilerini alır ve erişim belirteci ile yetkilendirme başlığını ayarlar:
- (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 ve AFNetworking'i kullanmanın doğru yolu bu mu? Ve bunun neden çalışmadığı hakkında bir fikrin var mı?
https://github.com/yellowfeather/rails-saas-ios https://github.com/AFNetworking/AFOAuth2Client/blob/master/AFOAuth2Client/AFOAuth2Client.h dosyasına bir göz atın. Üstte, bunu temel sınıfınız olarak kullanmamanızın önerildiği bir not vardır. – davidbitton