我不希望用户每次启动应用程序时都经过授权步骤。相反,我希望应用程序保存来自 3 条腿 OAuth 1.0a 进程的访问令牌,并使用它们来访问受保护的资源。
这是我到目前为止所拥有的,它每次都要求我授权(而不是登录)。
有任何想法吗?请帮忙。使用 GTMOauth 库。
- (void)signIn
{
activityIndicator.hidden = NO;
[activityIndicator startAnimating];
NSURL *requestURL = [NSURL URLWithString:@"http://webservice/oauth/request_token/"];
NSURL *accessURL = [NSURL URLWithString:@"http://webservice/oauth/access_token/"];
NSURL *authorizeURL = [NSURL URLWithString:@"http://webservice/oauth/authorize/"];
NSString *scope = @"http://webservice";
auth = [self getAuthForWebservice];
[auth setCallback:@"http://webservice/OAuthCallback"];
GTMOAuthViewControllerTouch *viewController;
viewController = [[GTMOAuthViewControllerTouch alloc] initWithScope:scope
language:nil
requestTokenURL:requestURL
authorizeTokenURL:authorizeURL
accessTokenURL:accessURL
authentication:auth
appServiceName:@"webservice"
delegate:self
finishedSelector:@selector(viewController:finishedWithAuth:error:)];
[[self navigationController] pushViewController:viewController
animated:YES];
[self navigationController].navigationBarHidden = YES;
}
- (void)viewController:(GTMOAuthViewControllerTouch *)viewController
finishedWithAuth:(GTMOAuthAuthentication *)auth
error:(NSError *)error {
if (error == nil) {
[self performSelector:@selector(doAnAuthenticatedAPIFetch)];
}
}
- (void)doAnAuthenticatedAPIFetch {
NSString *urlStr = @"http://webservice/resource/";
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[auth authorizeRequest:request];
NSError *error = nil;
NSURLResponse *response = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];