的使用flattr的API V2從我的iOS應用flattr的事我實現的簡短說明:
我目前使用 「谷歌工具箱爲Mac - 的OAuth 2個控制器」: http://code.google.com/p/gtm-oauth2/
創建令牌進行身份驗證:
- (GTMOAuth2Authentication *)flattrAuth {
NSURL *tokenURL = [NSURL URLWithString:@"https://flattr.com/oauth/token"];
// We'll make up an arbitrary redirectURI. The controller will watch for
// the server to redirect the web view to this URI, but this URI will not be
// loaded, so it need not be for any actual web page.
NSString *redirectURI = @"http://localhost/"; //for me localhost with/didn't work
GTMOAuth2Authentication *auth;
auth = [GTMOAuth2Authentication authenticationWithServiceProvider:@"MyApplication"
tokenURL:tokenURL
redirectURI:redirectURI
clientID:clientKey
clientSecret:clientSecret];
return auth;
}
創建一個視圖控制器進行身份驗證令牌:
- (GTMOAuth2ViewControllerTouch*)getSignInViewController{
GTMOAuth2Authentication *auth = [self flattrAuth];
// Specify the appropriate scope string, if any, according to the service's API documentation
auth.scope = @"flattr";
NSURL *authURL = [NSURL URLWithString:@"https://flattr.com/oauth/authorize"];
GTMOAuth2ViewControllerTouch *viewController;
viewController = [[[GTMOAuth2ViewControllerTouch alloc] initWithAuthentication:auth
authorizationURL:authURL
keychainItemName:keychainItemName
delegate:self
finishedSelector:@selector(viewController:finishedWithAuth:error:)] autorelease];
return viewController;
}
和委託方法:
- (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController
finishedWithAuth:(GTMOAuth2Authentication *)auth
error:(NSError *)error {
if (error != nil) {
DLog(@"Flattr sign-in failed with error: %@", [error localizedDescription]);
} else {
DLog(@"Flattr Signin success");
authToken = [auth retain];
}
}
您可以在應用程序中顯示的viewController - 它顯示flattr的登錄用戶,因此他可以驗證應用程序。
可以flattr的事與認證令牌是這樣的:
NSString* flattrURL = @"https://api.flattr.com/rest/v2/things/%qi/flattr";
NSURL* u = [NSURL URLWithString:[NSString stringWithFormat:flattrURL, item.flattrThingID]];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:u];
[authToken authorizeRequest:request completionHandler:^(NSError *error){
if (error == nil) {
// the request has been authorized
NSURLConnection* connection = [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];
if(!connection){
//TODO: handle error
} else {
[connection start];
}
} else {
//TODO: handle error
}
}];
現在實現NSURLConnectection委託方法,並解析JSON響應。
GTMOAuth2庫允許您將已驗證的令牌保存到鑰匙串。有關說明,請參閱http://code.google.com/p/gtm-oauth2/wiki/Introduction#Retrieving_Authorization_from_the_Keychain。
好像我找到了自己的解決方案使用GTM-的oauth2庫。我會檢查它是否真的起作用,並在明天描述解決方案。 – JanR 2011-12-18 22:20:32
期待說明。無論如何,我回答了你的問題,讓你知道我們打算oauth2流程。祝你好運,不要猶豫,問問是否不清楚! – 2011-12-19 07:54:15
對不起,今天不能回答。我希望明天下午回到它 – JanR 2011-12-19 19:59:10