13

I am using RestKit with an own OAuth2 Provider. I try to authenticate via Resource Owner Password Credentials.

Could anyone provide some example code and best practices for requesting protected resources via RestKit in general and authenticating via credentials to get an access token in specific?

Probably ResKit itself is not the best choice?

Community
  • 1
  • 1
pex
  • 7,351
  • 4
  • 32
  • 41

2 Answers2

9

You should check out this link: https://github.com/RestKit/RestKit/wiki/OAuth-Support-on-RestKit

You can try something like this (I do it in my app delegate):

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

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

Note: I haven't tested it because I use AuthenticationTypeHTTPBasic instead

Hope this helps!

Edit:

I found this code example that can help you more: https://github.com/rodchile/RestKit-OAuth2-Client-Example

lmt
  • 811
  • 1
  • 11
  • 27
  • Thanks for the helpful example and references. – Kenny Meyer Nov 18 '12 at 22:33
  • My pleasure to have helped you. I've also struggled with RestKit so I know what it is like. – lmt Nov 19 '12 at 00:39
  • The referenced code example at github/rodchile is not at all complete and uses a class RKClientOAuth that I do not see in the [0.2x docs](http://cocoadocs.org/docsets/RestKit/0.20.3/). The referenced wiki page does reference and outline a complete solution. – Douglas Lovell Mar 28 '14 at 08:39
0

After being fed up with overly sophisticated clients and APIs, I decided to do it myself and use a HTTP client.

With RubyMotion and BubbleWrap I have achieved using Resource Owner Password Credentials with an 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
Kenny Meyer
  • 7,849
  • 6
  • 45
  • 66