我正在尝试验证具有3个不同服务器端点的API:
当AFOAuth1Client需要使用baseURL进行初始化时,会出现此问题。我试过@“”,nil和@“http://”,这些都会在AFOAuth1Client初始化中触发崩溃。
我的问题是:我如何创建一个具有nUR的baseURL的AFOAuth1Client实例,或者如何更改authorizeUsingOAuthWithRequestTokenPath
的参数以允许我使用正确的路径? (授权URL有www。而请求URL和访问URL没有www。)。
AFOAuth1Client *client = [[AFOAuth1Client alloc] initWithBaseURL:[NSURL URLWithString:@""]key:kConsumerKey secret:kConsumerSecret];
[client authorizeUsingOAuthWithRequestTokenPath:@"http://api.XXX.com/oauth/request_token" userAuthorizationPath:@"http://www.XXX.com/oauth/authorize" callbackURL:nil accessTokenPath:@"http://api.XXX.com/oauth/access_token" accessMethod:@"Identity"
success:^(AFOAuth1Token *accessToken) {
NSLog(@"successful login");
} failure:^(NSError *error) {
NSLog(@"could not login error %@", error);
}];
答案 0 :(得分:0)
第一步是编辑AFHTTPClient接口文件。
将@property baseUrl更改为:
@property (readwrite, nonatomic, retain) NSURL *baseURL;
这允许我们根据输入的当前地址使baseURL动态化。在我的案例中AuthorizeURL / Request Token / Access Token。
接下来,我更新了AFOAuth1Client类,并通过使用正确的baseURL更新以下方法手动更改了baseURL:
(void)authorizeUsingOAuthWithRequestTokenPath:(NSString *)requestTokenPath
userAuthorizationPath:(NSString *)userAuthorizationPath
callbackURL:(NSURL *)callbackURL
accessTokenPath:(NSString *)accessTokenPath
accessMethod:(NSString *)accessMethod
success:(void (^)(AFOAuth1Token *accessToken))success
failure:(void (^)(NSError *error))failure
...
//set baseURL
self.baseURL = [NSURL URLWithString:@"http://www.XXX.com/"];
[[UIApplication sharedApplication] openURL:[[self requestWithMethod:@"GET" path:userAuthorizationPath parameters:parameters] URL]];
//reset it back
self.baseURL = [NSURL URLWithString:@"http://api.XXX.com/"];