到目前为止,我的 iOS 应用程序运行良好,但今天我遇到了“我们无法找到授权令牌”的问题。有时会发生这种情况(大多数情况下它工作正常)。奇怪的是,当我尝试在我的桌面上登录我的 LinkedIn 帐户时,我第一次遇到了同样的问题。我在许多应用程序中实现了相同的代码,但所有应用程序都在工作很好。但是今天我在我的新应用程序中遇到了这个问题。
代码片段:
请求令牌:
- (void)requestTokenFromProvider
{
LOAMutableURLRequest *request =
[[[LOAMutableURLRequest alloc] initWithURL:requestTokenURL
consumer:self.consumer
token:nil
callback:linkedInCallbackURL
signatureProvider:nil] autorelease];
[request setHTTPMethod:@"POST"];
LOARequestParameter *nameParam = [[LOARequestParameter alloc] initWithName:@"scope" value:@"r_fullprofile+w_messages+r_network+r_emailaddress+rw_nus"];
NSArray *params = [NSArray arrayWithObjects:nameParam, nil];
[request setParameters:params];
LOARequestParameter * scopeParameter=[LOARequestParameter requestParameter:@"scope" value:@"r_fullprofile w_messages r_network r_emailaddress rw_nus"];
[request setParameters:[NSArray arrayWithObject:scopeParameter]];
LOADataFetcher *fetcher = [[[LOADataFetcher alloc] init] autorelease];
[fetcher fetchDataWithRequest:request
delegate:self
didFinishSelector:@selector(requestTokenResult:didFinish:)
didFailSelector:@selector(requestTokenResult:didFail:)];
}
- (void)requestTokenResult:(LOAServiceTicket *)ticket didFinish:(NSData *)data
{
if (ticket.didSucceed == NO)
return;
NSString *responseBody = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
self.requestToken = [[LOAToken alloc] initWithHTTPResponseBody:responseBody];
[responseBody release];
[self allowUserToLogin];
}
- (void)requestTokenResult:(LOAServiceTicket *)ticket didFail:(NSData *)error
{
NSLog(@"%@",[error description]);
}
Linkedin 登录页面和访问令牌:
- (void)allowUserToLogin
{
NSString *userLoginURLWithToken = [NSString stringWithFormat:@"%@?oauth_token=%@",
userLoginURLString, self.requestToken.key];
userLoginURL = [NSURL URLWithString:userLoginURLWithToken];
NSURLRequest *request = [NSMutableURLRequest requestWithURL: userLoginURL];
[webView loadRequest:request];
}
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = request.URL;
NSString *urlString = url.absoluteString;
addressBar.text = urlString;
[activityIndicator startAnimating];
BOOL requestForCallbackURL = ([urlString rangeOfString:linkedInCallbackURL].location != NSNotFound);
if ( requestForCallbackURL )
{
BOOL userAllowedAccess = ([urlString rangeOfString:@"user_refused"].location == NSNotFound);
if ( userAllowedAccess )
{
[self.requestToken setVerifierWithUrl:url];
[self accessTokenFromProvider];
}
else
{
// User refused to allow our app access
// Notify parent and close this view
// [[NSNotificationCenter defaultCenter]
// postNotificationName:@"loginViewDidFinish"
// object:self
// userInfo:nil];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"LinkedInCancelled"];
[[ImpActivityAgent defaultAgent] makeBusy:NO];
[self dismissViewControllerAnimated:NO completion:nil];
}
}
else
{
// Case (a) or (b), so ignore it
}
return YES;
}
- (void)accessTokenFromProvider
{
[[NSUserDefaults standardUserDefaults] setObject:self.consumer forKey:@"LinkedinConsumer"];
LOAMutableURLRequest *request =
[[[LOAMutableURLRequest alloc] initWithURL:accessTokenURL
consumer:self.consumer
token:self.requestToken
callback:nil
signatureProvider:nil] autorelease];
[request setHTTPMethod:@"POST"];
LOADataFetcher *fetcher = [[[LOADataFetcher alloc] init] autorelease];
[fetcher fetchDataWithRequest:request
delegate:self
didFinishSelector:@selector(accessTokenResult:didFinish:)
didFailSelector:@selector(accessTokenResult:didFail:)];
}
- (void)accessTokenResult:(LOAServiceTicket *)ticket didFinish:(NSData *)data
{
NSString *responseBody = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
[[NSUserDefaults standardUserDefaults] setObject:responseBody forKey:@"AccessTokenresponseBody"];
BOOL problem = ([responseBody rangeOfString:@"oauth_problem"].location != NSNotFound);
if ( problem )
{
NSLog(@"%@",responseBody);
}
else
{
self.accessToken = [[LOAToken alloc] initWithHTTPResponseBody:responseBody];
[[NSUserDefaults standardUserDefaults] setObject:responseBody forKey:@"accessToken"];//save here
[[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"TokenRefreshDate"];//save here
[[NSUserDefaults standardUserDefaults] synchronize];
}
// Notify parent and close this view
[[NSNotificationCenter defaultCenter]
postNotificationName:@"loginViewDidFinish"
object:self];
[self dismissViewControllerAnimated:NO completion:nil];
[responseBody release];
}
请求网址:
- (void)initLinkedInApi
{
apikey = @"vwu3pmtjaxyz";
secretkey = @"XkPxP1DNANMg0Dzq";
self.consumer = [[LOAConsumer alloc] initWithKey:apikey
secret:secretkey
realm:@"http://api.linkedin.com/"];
requestTokenURLString = @"https://api.linkedin.com/uas/oauth/requestToken";
accessTokenURLString = @"https://api.linkedin.com/uas/oauth/accessToken";
userLoginURLString = @"https://www.linkedin.com/uas/oauth/authorize";
linkedInCallbackURL = @"hdlinked://linkedin/oauth";
requestTokenURL = [[NSURL URLWithString:requestTokenURLString] retain];
accessTokenURL = [[NSURL URLWithString:accessTokenURLString] retain];
userLoginURL = [[NSURL URLWithString:userLoginURLString] retain];
}
有人可以告诉我这背后的原因是什么吗?