當用戶登錄到應用程序時,您應該通過LinkedIn API請求訪問令牌。之後,您使用NSUserDefaults來保存此訪問令牌。因此,當用戶打開應用程序時,您會檢查accesstoken是否存在。如果accesstoken存在,用戶已經登錄到應用程序。
- (void)getAccessToken:(NSString *)authorizationCode success:(void (^)(NSDictionary *))success failure:(void (^)(NSError *))failure
{
NSString *accessTokenUrl = @"/uas/oauth2/accessToken?grant_type=authorization_code&code=%@&redirect_uri=%@&client_id=%@&client_secret=%@";
NSString *url = [NSString stringWithFormat:accessTokenUrl, authorizationCode, [self.application.redirectURL LIAEncode], self.application.clientId, self.application.clientSecret];
[self POST:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *accessToken = [responseObject objectForKey:@"access_token"];
NSTimeInterval expiration = [[responseObject objectForKey:@"expires_in"] doubleValue];
// store credentials
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:accessToken forKey:LINKEDIN_TOKEN_KEY];
[userDefaults setDouble:expiration forKey:LINKEDIN_EXPIRATION_KEY];
[userDefaults setDouble:[[NSDate date] timeIntervalSince1970] forKey:LINKEDIN_CREATION_KEY];
[userDefaults synchronize];
success(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
failure(error);
}];
}
我沒有使用該庫,但oauth的重要組成部分是在auth期間獲取訪問令牌並使用它來簽署後續請求。那個圖書館以某種方式這樣做。在那裏應該有一個顯着的訪問令牌getter和setter。一旦你發現,在第一次認證之後,在下一次應用程序啓動時,在設備上堅持令牌,讀取該值並重新設置。 – danh
我應該如何在設備上堅持令牌?以某種方式使用NSUserDefaults? – user2282749
這是一種方式,或文件系統或鑰匙串服務(https://developer.apple.com/library/ios/#documentation/security/conceptual/keychainServConcepts/iPhoneTasks/iPhoneTasks.html%23//apple_ref/doc/ uid/TP30000897-CH208-SW1) – danh