我知道这似乎是一个愚蠢的问题,URL解析很容易搜索,但我真的很难从下面的给定网址获取访问令牌。
我的代码
NSString* u = [[[[frame dataSource]request]URL]absoluteString];
NSString* output = nil;
if([u hasPrefix:@"https://login.live.com/oauth20_desktop.srf"])
{
NSMutableDictionary *queryStringDictionary = [[NSMutableDictionary alloc] init];
NSArray *urlComponents = [u componentsSeparatedByString:@"&"];
for (NSString *keyValuePair in urlComponents)
{
NSArray *pairComponents = [keyValuePair componentsSeparatedByString:@"="];
NSString *key = [[pairComponents firstObject] stringByRemovingPercentEncoding];
NSString *value = [[pairComponents lastObject] stringByRemovingPercentEncoding];
[queryStringDictionary setObject:value forKey:key];
}
}
结果
{
"expires_in" = 3600;
"https://login.live.com/oauth20_desktop.srf?lc" = "EwCIAq1DBAAUGCCXc8wU/zFu9QnLdZXy+YnElFkAAYQSbBaTYP1E4GlmB6vPpa9noNLSSg+FzA88BgdO2lFbr3EzR9POPhf417OAqXB7dlhZ7q/3AEhxthlBwFb3IebkElbeKUZll0QeIf7HniteFGJA0nD81xv01CHwjnJ0g/eIuh2GH8kE8/dL9seRMN4ngayEpV4Kp5/k3o1z9nDhtmn9/pZfspIlXo75Km9n2hqPAGt+7przBmYZZZGgLiQitQDCQEBidgKDfLjBmO5ZjbYFGuwCJz1Hmlsbs+e45MuF+VF4uk7ocAu8CQnFrYdVusPPS04QR2EzUkGt3uEBM7jmqnjZfM+EJ78kggmuPXuxkMarNsGiMYdzigNxIHEDZgAACE0dTsiQuAaFWAFL7tvmsSOprKe2jx4FrRgZhWn5F9vQTKw9thK63urFSY79m/CbLqQSlEMKLi4mRptJa+tXKFpg9anxHMyfvMSDQ5ryWK291qJX5W5Yv5syxSm3u6PEo8KM9WmeAdCW/trJzrCIVPrSdshzvI2N5Iu+SP9igWGDoWdE6uoH7G0ZA8gPn2v3NagzVNKUyIKuceiT3fI20nBCYx/6+N5Z/WWKmmkyuuAqo3kH5+FrGz2lz4F5tP0UOdYjY6q73RqUQMLwdJ2/3UNb3BcE/98nGbOgKMtCPPCx0GaPBwOPlVkvxqMa6oeNaHmArZNsyhwuvJEfWEbB+HLXtE5gYG5PziybUDeLa+uIJda9z0Tb9HRyMb+I2nA7+SMkhgITyo2OplaTMRVrKAGHViEDVSBMbspkbG6tV/nk/7SC3/cICXGPFX9u29sEcGe6l0onMQougQvhuXs90CFVNkVG8B";
scope = "wl.signin wl.offline_access onedrive.readwrite wl.basic";
"token_type" = bearer;
"user_id" = 6c0a5d79b74cefa0c6d9d5f302a14876;
}
问题
正如您在上面的结果中所看到的,有一个键 https://login.live.com/oauth20_desktop.srf?lc 我认为它应该是 Access_Token
任何建议都表示赞赏。非常感谢你。
答案 0 :(得分:2)
我的简单解决方案:
NSString *url = @"https://login.live.com/oauth20_desktop.srf?lc=1033#access_token=EwCIAq1DBAAUGCCXc8wU/zFu9QnLdZXy%2bYnElFkAAcXV/IZULfKdn7RHvXY8CmB1J2c0PgYOldbBLp023y1BilHNHPQBeAflk4edFfbK2Fwdazg/mFi900PbS013mQqEE93d/4NlutyY9MK%2bU/q0QjInVfTPdxAQvc5RLevnMsqiS7H77Eg9K5YJCjQKTGE4ILhRO60UEyIXU2GgL4eNmYX3%2b64wrnL5q3QBmD%2bXFgkkk6EKvbncUdUeGWj%2bzlgKSiXT06QMK1LKhcFFrrOQj3spgnkyx5LmrDsBcDIAUxrMZ%2bF3kwutGEj7nfZTovuR/FT4XE221wLXZEpSydIwi20EGiGcYBsWw5mPqCRvPShL3dYd0%2bMLczvmu0iTQgK3kYDZgAACD9QFp4Vqf%2b9WAE2rBZLy%2bGoHEP3vivVKIYz2znPU/IRmNGmq8FRLhrwm/YcByRfIQOj9AGoqwL0wsgTj5lD4DIwlPWREI1zj%2bAKTGDZilkewxzncpwynF5ZAxHrT3iEjqyqAIpvtzY1xAiGj8Swqu1uLcEUS6p7c/Qk2HiSc1GY6%2bnc9epmkhSxIGi9HHTmnREaRxgbjbqLDZ5%2bLYhQLu9k%2bHjadfwoKFQpquN3NjbVi3LpZpHb2gfhr%2bf17XcLhEHb1xQnNg7rVVbvGe8FAgp9kZrkzzbFXY8jKzsjfRW%2bIUkNvcnLD39eFDs%2b/og2nxT8jE/qkKLW9wfgcDkfMnGWpIvQrCT6XrqvDaDCEVJ0rCiUG/L33SELLlBP3g8fLNjUOZ5K0ztksreaSH/4YCQdnS%2bopl2EG7PFl8ckDfzvVO5RqZS/SoSpa9h61regG37qzjLf2d%2bvxUPhW2FepeUwA28B&token_type=bearer&expires_in=3600&scope=wl.signin%20wl.offline_access%20onedrive.readwrite%20wl.basic&user_id=6c0a5d79b74cefa0c6d9d5f302a14876";
NSURLComponents *components = [NSURLComponents componentsWithString:[url stringByReplacingOccurrencesOfString:@"#" withString:@"&"]];
//get query items
NSLog(@"%@", components.queryItems);
结果将是:
(
"<NSURLQueryItem 0x17541ae0> {name = lc, value = 1033}",
"<NSURLQueryItem 0x1753f7f0> {name = access_token, value = EwCIAq1DBAAUGCCXc8wU/zFu9QnLdZXy+YnElFkAAcXV/IZULfKdn7RHvXY8CmB1J2c0PgYOldbBLp023y1BilHNHPQBeAflk4edFfbK2Fwdazg/mFi900PbS013mQqEE93d/4NlutyY9MK+U/q0QjInVfTPdxAQvc5RLevnMsqiS7H77Eg9K5YJCjQKTGE4ILhRO60UEyIXU2GgL4eNmYX3+64wrnL5q3QBmD+XFgkkk6EKvbncUdUeGWj+zlgKSiXT06QMK1LKhcFFrrOQj3spgnkyx5LmrDsBcDIAUxrMZ+F3kwutGEj7nfZTovuR/FT4XE221wLXZEpSydIwi20EGiGcYBsWw5mPqCRvPShL3dYd0+MLczvmu0iTQgK3kYDZgAACD9QFp4Vqf+9WAE2rBZLy+GoHEP3vivVKIYz2znPU/IRmNGmq8FRLhrwm/YcByRfIQOj9AGoqwL0wsgTj5lD4DIwlPWREI1zj+AKTGDZilkewxzncpwynF5ZAxHrT3iEjqyqAIpvtzY1xAiGj8Swqu1uLcEUS6p7c/Qk2HiSc1GY6+nc9epmkhSxIGi9HHTmnREaRxgbjbqLDZ5+LYhQLu9k+HjadfwoKFQpquN3NjbVi3LpZpHb2gfhr+f17XcLhEHb1xQnNg7rVVbvGe8FAgp9kZrkzzbFXY8jKzsjfRW+IUkNvcnLD39eFDs+/og2nxT8jE/qkKLW9wfgcDkfMnGWpIvQrCT6XrqvDaDCEVJ0rCiUG/L33SELLlBP3g8fLNjUOZ5K0ztksreaSH/4YCQdnS+opl2EG7PFl8ckDfzvVO5RqZS/SoSpa9h61regG37qzjLf2d+vxUPhW2FepeUwA28B}",
"<NSURLQueryItem 0x1753f3d0> {name = token_type, value = bearer}",
"<NSURLQueryItem 0x1753f660> {name = expires_in, value = 3600}",
"<NSURLQueryItem 0x1753fd60> {name = scope, value = wl.signin wl.offline_access onedrive.readwrite wl.basic}",
"<NSURLQueryItem 0x1757daf0> {name = user_id, value = 6c0a5d79b74cefa0c6d9d5f302a14876}"
)
/// the given below code will parse url components and get you the access token in a NSString variable]
NSArray *queryItems = components.queryItems;
NSString *TheToken = [self valueForKey:@"access_token" fromQueryItems:queryItems];
NSLog(TheToken);
/// add this funciton in you .m file
- (NSString *)valueForKey:(NSString *)key
fromQueryItems:(NSArray *)queryItems
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name=%@", key];
NSURLQueryItem *queryItem = [[queryItems filteredArrayUsingPredicate:predicate]
firstObject];
return queryItem.value;
}