簡體   English   中英

在iOS中使用Flattr API v2

[英]Using Flattr API v2 with iOS

我目前正在構建一個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庫?

使用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上的介紹。

當您不想驗證桌面/移動應用程序時,您將不會使用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=tokenhttps://flattr.com/oauth/authorize?client_id=2134&redirect_uri=iphone-application://oauth-callback&response_type=token application: 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庫,但我不知道它是否有用。

這個看起來不錯: https//github.com/neonichu/FlattrKit

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM