2012-03-28 64 views

回答

9

你应该检查出此链接:https://github.com/RestKit/RestKit/wiki/OAuth-Support-on-RestKit

你可以尝试这样的事情(我在我的应用程序委托):

RKObjectManager *manager = [RKObjectManager objectManagerWithBaseURLString:@"http://www.yourdomain.com"]; 
[manager setSerializationMIMEType:RKMIMETypeJSON]; 

[[RKClient sharedClient] setAuthenticationType:RKRequestAuthenticationTypeOAuth2]; 
[[RKClient sharedClient] setUsername:@"username"]; 
[[RKClient sharedClient] setPassword:@"password"]; 

注意:我没有测试它,因为我使用AuthenticationTypeHTTPBasic而不是

希望这有助于!

编辑:

我发现这个代码示例,可以帮助您更多:

+0

谢谢你的帮助的例子和参考。 – 2012-11-18 22:33:42

+0

我很高兴帮助你。我也在用RestKit挣扎,所以我知道它是什么样子。 – 2012-11-19 00:39:30

+0

github/rodchile中引用的代码示例根本没有完成,并且使用了在[0.2x文档](http://cocoadocs.org/docsets/RestKit/0.20.3/)中没有看到的类RKClientOAuth。引用的维基页面确实提供了一个完整的解决方案。 – 2014-03-28 08:39:28

0

在厌倦了非常复杂的客户端和API之后,我决定自己做,并使用HTTP客户端。

使用RubyMotion和BubbleWrap我已经实现了使用资源所有者密码凭证和API。

params = { 
    "grant_type" => "password", 
    "username" => "<USERNAME>", 
    "password" => "<PASSWORD>", 
    "client_id" => "<SOMETHING>", 
    "client_secret" => "<SOMETHING>", 
    "scope" => "public write" 
} 

BW::HTTP.post("https://example.com/oauth/token", 
       :payload => params, 
       :headers => {}) do |response| 

    if response.ok? 
    # Do something 
    end 
end