此 Objective-C 封装提供了请求和刷新需要 OAuth 1.0 认证的 API 访问令牌的功能。
要运行示例项目,请克隆仓库,然后首先从 Example 目录运行 pod install
。
在使用 CDOAuth1Kit 之前,您需要将您的应用程序注册到您希望从其获取 OAuth 1.0 授权的 API。传统上,您将提供一些基本的应用程序信息(名称、描述等)和一个 回调 URL,这些将在 OAuth 过程中使用。回调 URL 可以是一个 Web URL(例如 https://www.myapplicationwebsite.com)或一个移动回调 URL(myApplicationName://oauthRequest)。以下是如何在 Xcode 中定义一个移动回调 URL 的说明。您的应用程序批准后,API 将提供 消费者密钥 和 消费者密钥,这两者在 OAuth 过程中都将使用。
NSURL *apiOAuthURL = [NSURL URLWithString:@"https://api.login.thisisafakeurl.com/oauth"]
self.oAuth1SessionManager = [[CDOAuth1SessionManager alloc] initWithBaseURL:[NSURL URLWithString:apiOAuthURL]
consumerKey:consumerKey
consumerSecret:consumerSecret];
一旦创建了 CDOAuth1SessionManager 对象,您就可以从您注册应用程序的 API 中请求授权。
下面的截图显示了如何在 Xcode 中定义一个移动回调 URL。
OAuth 1.0 授权过程中的第一步是让应用程序从 API 接收一个 OAuth 请求令牌。以下方法是获取 OAuth 请求令牌的。
[self.oAuth1SessionManager fetchRequestTokenWithPath:@"get_request_token"
method:@"POST"
callbackURL:@"myApplicationName://oauthRequest"
scope:nil
success:^(CDOAuth1Credential *requestToken) {
NSString *authURL = [NSString stringWithFormat:@"https://api.login.thisisafakeurl.com/oauth/authorize?oauth_token=%@", requestToken.token];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:authURL]];
} failure:^(NSError *error) {
NSLog(@"Fetch Request Token Error: %@", error.localizedDescription);
}];
从 API 成功接收 OAuth 请求令牌后,您可以允许用户提供授权,使您的应用程序可以通过 API 获取数据。传统上,该 URL 会期望请求令牌作为参数。
一旦用户允许调用您的应用程序,API的OAuth授权网页将重定向到您在用API注册应用程序时提供的OAuth回调URL。该回调URL将在您的应用AppDelegate类中触发以下方法。
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
// Handle response
}
您需要将上述方法添加到您的AppDelegate类中,因为它不是在创建应用程序期间添加的预定义方法之一。OAuth过程下一步要求应用程序从API接收OAuth访问令牌。以下代码行可以用来获取OAuth访问令牌(这将替换上述方法中的// 处理响应)。
// Check that the url that opened your application was the OAuth callback URL
if ([CDOAuth1Helper isAuthorizationCallbackURL:url
callbackURLScheme:@"myApplicationName"
callbackURLHost:@"oauthRequest"] == YES) {
// Get the request token from the OAuth callback URL query parameters
CDOAuth1Credential *requestToken = [CDOAuth1Credential credentialWithQueryString:url.query];
// Request an OAuth access token
[self.oAuth1SessionManager fetchAccessTokenWithPath:@"get_token"
method:@"POST"
requestToken:requestToken
success:^(CDOAuth1Credential *accessToken) {
[self.oAuth1SessionManager.requestSerializer saveAccessToken:accessToken];
} failure:^(NSError *error) {
NSLog(@"Fetch Access Token Error: %@", error.localizedDescription);
}];
return YES;
} else {
return NO;
}
从API成功接收OAuth访问令牌后,您将能够使用CDOAuth1SessionManager对象查询API的相关端点来检索数据。
大多数API都会在OAuth访问令牌上设置过期日期。一旦OAuth访问令牌已过期,就需要一个新的令牌以继续从API成功检索数据。以下方法用于刷新OAuth访问令牌。
// Get the expired OAuth access token
CDOAuth1Credential *accessToken = self.oAuth1SessionManager.requestSerializer.accessToken;
// Add any additional parameters required by the API to refresh the OAuth access token.
NSDictionary *parameters = @{
@"oauth_session_handle": accessToken.userInfo[@"oauth_session_handle"]
};
// Refresh the OAuth access token
[self.oAuth1SessionManager refreshAccessTokenWithPath:@"get_token"
parameters:parameters
method:@"POST"
accessToken:accessToken
success:^(CDOAuth1Credential *accessToken) {
[self.oAuth1SessionManager.requestSerializer saveAccessToken:accessToken];
} failure:^(NSError *error) {
NSLog(@"Refresh Access Token Error: %@", error.localizedDescription);
}];
在从API成功收到刷新后的OAuth访问令牌后,您可以使用CDOAuth1SessionManager对象继续通过查询API的相关端点来检索数据。
Christopher de Haan,[email protected]
CDOAuth1Kit受到了由BDBOAuth1SessionManager的影响,这是一个由Bradley David Bergeron开发的OAuth 1.0库。
CDOAuth1Kit增加了以下功能
CDOAuth1Kit可在MIT许可证下使用。有关更多信息,请参阅LICENSE文件。