This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
BGLastFmWebServiceHandshaker.m
78 lines (61 loc) · 2.32 KB
/
BGLastFmWebServiceHandshaker.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//
// BGLastFmWebServiceHandshaker.m
// ApiHubTester
//
// Created by Ben Gummer on 17/07/08.
// Copyright 2008 Ben Gummer. All rights reserved.
//
#import "BGLastFmWebServiceHandshaker.h"
#import "BGLastFmWebServiceParameterList.h"
#import "BGLastFmWebServiceCaller.h"
#import "BGLastFmWebServiceResponse.h"
#import "HubStrings.h"
#import "HubNotifications.h"
#import "Defines.h"
@interface BGLastFmWebServiceHandshaker (Private)
-(NSString *)sessionKeyFromToken:(NSString *)theToken;
@end
@implementation BGLastFmWebServiceHandshaker (Private)
-(NSString *)sessionKeyFromToken:(NSString *)theToken {
NSLog(@"SESSION FROM TOKEN %@",theToken);
if (theToken) {
BGLastFmWebServiceParameterList *sessionParams = [[BGLastFmWebServiceParameterList alloc] initWithMethod:@"auth.getSession" andSessionKey:nil];
[sessionParams setParameter:theToken forKey:@"token"];
BGLastFmWebServiceCaller *sessionCaller = [[BGLastFmWebServiceCaller alloc] init];
BGLastFmWebServiceResponse *response = [sessionCaller callWithParameters:sessionParams usingPostMethod:NO usingAuthentication:YES];
[sessionCaller release];
[sessionParams release];
NSLog(@"%@",response.responseDocument);
if (response.wasOK) {
NSString *username = [response stringValueForXPath:@"/lfm/session/name"];
NSString *session = [response stringValueForXPath:@"/lfm/session/key"];
if (session && username) {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:session forKey:BGWebServiceSessionKey];
[defaults setObject:username forKey:BGPrefUsername];
[[NSNotificationCenter defaultCenter] postNotificationName:APIHUB_WebServiceAuthorizationProcessing object:nil];
return session;
}
} else {
NSLog(@"Unable to fetch Session Key: response.lastFmCode = %d",response.lastFmCode);
}
}
return nil;
}
@end
#pragma mark NORMAL
@implementation BGLastFmWebServiceHandshaker
-(id)init {
self = [super init];
return self;
}
-(void)openAuthorizationSite {
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.last.fm/api/auth?api_key=%@",API_KEY]]];
}
+(NSString *)fetchSessionKeyUsingToken:(NSString *)theToken {
BGLastFmWebServiceHandshaker *obj = [[self alloc] init];
NSString *sk = [obj sessionKeyFromToken:theToken];
[obj release];
return sk;
}
@end