2011-12-18 84 views
3

我目前正在構建一個iOS應用程序,並希望在Flattr-API v2中包含Flattr-Support。使用Flattr API v2與iOS

我已經在https://flattr.com/apps/創建了我的應用程序,並獲得了關鍵和祕密。

問題是我必須在flattr的應用程序設置中提供一個回調URL,即使我選擇「client」作爲應用程序類型。此外,在輸入字段中似乎只允許http:// ... callback-URL,因此我無法設置回調URL來打開我的應用程序(類似myApp:// ...)

我是否爲客戶端應用程序實施Flattr oAuth過程? 是否有任何詳細說明如何使用非基於Web的/ iOS應用程序實現flattr認證?

我打算使用JDG OAuthConsumer庫,但這似乎不起作用 - 我可以使用任何其他iOS librarys?

+0

好像我找到了自己的解決方案使用GTM-的oauth2庫。我會檢查它是否真的起作用,並在明天描述解決方案。 – JanR 2011-12-18 22:20:32

+0

期待說明。無論如何,我回答了你的問題,讓你知道我們打算oauth2流程。祝你好運,不要猶豫,問問是否不清楚! – 2011-12-19 07:54:15

+0

對不起,今天不能回答。我希望明天下午回到它 – JanR 2011-12-19 19:59:10

回答

1

當您不想驗證桌面/移動應用程序時,您不會使用oauth2隱式授權流程。當你註冊你的flattr應用程序時,使用一個專用的URI來回調你的應用程序,例如。 iphone-application://oauth-callback

當您向我們驗證應用程序時,您使用response_typetoken而不是code。這將立即創建一個令牌並將您重定向回您的應用程序。

Ex。請求URL:https://flattr.com/oauth/authorize?client_id=2134&redirect_uri=iphone-application://oauth-callback&response_type=token

如果資源所有者將授權您的應用程序,我們將發送HTTP 302並將您重定向到您的重定向uri。

Ex。響應302位置:iphone-application://oauth-callback#access_token=e5oNJ4917WAaJaO4zvoVV2dt3GYClPzp&token_type=bearer

目前我們沒有任何詳細的文檔解釋如何執行隱式授權,但我們正在處理文檔。同時我全部耳朵。

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

+0

這就是我最初認爲它應該工作。但是你確定我可以在applcation設置中設置iphone-application:// oauth-callback嗎? 只有我能夠設置的URL是http:// ... – JanR 2011-12-19 19:57:41

+0

我更改了應用程序頁面以接受任何URI。 :) – 2011-12-20 08:52:09

3

的使用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

+0

謝謝您的分享!我很高興你的工作能夠順利進行,並且我們可以確定一些錯誤。 – 2011-12-23 17:27:00