在iOS中使用Flattr API v2

时间:2011-12-18 21:01:31

标签: ios oauth flattr

我目前正在构建一个iOS应用程序,并希望在Flattr-API v2上包含Flattr-Support。

我已经在https://flattr.com/apps/创建了我的申请,并获得了密钥和秘密。

问题是我必须在flattr的应用程序设置中提供一个回调URL,即使我选择“client”作为应用程序类型。此外,输入字段中似乎只允许使用http:// ... callback-URLs,因此我无法设置回调URL来打开我的应用程序(类似于myApp:// ...)

如何为客户端应用程序实现Flattr oAuth流程? 是否有如何使用非基于Web的/ iOS应用程序实现flattr-authentication的详细说明?

我计划使用JDG OAuthConsumer库,但这似乎不起作用 - 我可以使用的任何其他iOS库?

3 个答案:

答案 0 :(得分:3)

使用Flattr API v2对我的iOS应用程序中的内容进行详细说明的实现的简短描述:

我目前正在使用" Google Toolbox for 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;
}

创建一个ViewController来验证令牌:

- (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-login,以便他可以对应用程序进行身份验证。

您可以通过以下方式使用身份验证令牌展示事物:

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的介绍以获取相关说明。

答案 1 :(得分:1)

当您不想验证桌面/移动应用程序时,您将不会使用oauth2隐式授权流程。当您注册flattr应用程序时,请使用将回调到您的应用程序的特定于应用程序的URI,例如。 iphone-application://oauth-callback

当您向我们验证应用程序时,请使用response_type token代替code。这将立即创建一个令牌,并将您重定向回您的应用程序。

实施例。请求网址:https://flattr.com/oauth/authorize?client_id=2134&redirect_uri=iphone-application://oauth-callback&response_type=token

如果资源所有者将授权您的应用程序,我们将发送HTTP 302并将您重定向回您的重定向uri。

实施例。回复302地点:iphone-application://oauth-callback#access_token=e5oNJ4917WAaJaO4zvoVV2dt3GYClPzp&token_type=bearer

目前我们没有任何详细的文档说明如何进行隐式授权,但我们正在编写文档。与此同时,我全都耳朵。

https://github.com/nxtbgthng/OAuth2Client是iOS oauth2库,但我不知道它是否有用。

答案 2 :(得分:0)

这个看起来不错:https://github.com/neonichu/FlattrKit