我目前正在构建一个 iOS 应用程序,并希望在 Flattr-API v2 上包含 Flattr-Support。
我已经在 https://flattr.com/apps/ 创建了我的应用程序并得到了 key 和 secret 。
问题是即使我选择“客户端”作为应用程序类型,我也必须在 flattr 的应用程序设置中提供回调 URL。此外,输入字段中似乎只允许使用 http://... 回调 URL,因此我无法设置回调 URL 来打开我的应用程序(类似于 myApp://...)
如何为客户端应用程序实现 Flattr oAuth 流程? 是否有任何详细说明如何使用非基于 Web 的/iOS 应用程序实现 flattr 身份验证?
我计划使用 JDG OAuthConsumer 库,但这似乎不起作用 - 我可以使用任何其他 iOS 库吗?
最佳答案
我使用 Flattr API v2 从我的 iOS 应用程序扁平化一个东西的实现的简短描述:
我目前正在使用“适用于 Mac 的 Google 工具箱 - OAuth 2 Controller ”: http://code.google.com/p/gtm-oauth2/
创建要认证的Token:
- (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;
}
创建一个 ViewController 来验证 token :
- (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;
}
和委托(delegate)方法:
- (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-login,以便他可以对应用程序进行身份验证。
您可以通过这种方式使用身份验证 token 来扁平化:
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 委托(delegate)方法并解析 JSON 响应。
GTMOAuth2 库允许您将经过身份验证的 token 保存到钥匙串(keychain)中。看看他们的介绍http://code.google.com/p/gtm-oauth2/wiki/Introduction#Retrieving_Authorization_from_the_Keychain获取说明。
关于ios - 在 iOS 上使用 Flattr API v2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8554779/