Inherits from AFHTTPClient
Declared in AFOAuth2Client.h

Overview

AFOAuth2Client encapsulates common patterns to authenticate against a resource server conforming to the behavior outlined in the OAuth 2.0 specification.

In your application, it is recommended that you use AFOAuth2Client exclusively to get an authorization token, which is then passed to another AFHTTPClient subclass.

Tasks

Accessing OAuth 2 Client Properties

  •   serviceProviderIdentifier

    The service provider identifier used to store and retrieve OAuth credentials by AFOAuthCredential. Equivalent to the hostname of the client baseURL.

    property
  •   clientID

    The client identifier issued by the authorization server, uniquely representing the registration information provided by the client.

    property

Creating and Initializing OAuth 2 Clients

Authenticating

Properties

clientID

@property (readonly, nonatomic) NSString *clientID
Discussion

The client identifier issued by the authorization server, uniquely representing the registration information provided by the client.

Declared In

AFOAuth2Client.h

serviceProviderIdentifier

@property (readonly, nonatomic) NSString *serviceProviderIdentifier
Discussion

The service provider identifier used to store and retrieve OAuth credentials by AFOAuthCredential. Equivalent to the hostname of the client baseURL.

Declared In

AFOAuth2Client.h

Class Methods

clientWithBaseURL:clientID:secret:

+ (instancetype)clientWithBaseURL:(NSURL *)url clientID:(NSString *)clientID secret:(NSString *)secret
Discussion

Creates and initializes an AFOAuth2Client object with the specified base URL, client identifier, and secret.

Parameters

url

The base URL for the HTTP client. This argument must not be nil.

clientID

The client identifier issued by the authorization server, uniquely representing the registration information provided by the client.

secret

The client secret.

Return Value

The newly-initialized OAuth 2 client

Declared In

AFOAuth2Client.h

Instance Methods

authenticateUsingOAuthWithPath:code:redirectURI:success:failure:

- (void)authenticateUsingOAuthWithPath:(NSString *)path code:(NSString *)code redirectURI:(NSString *)uri success:(void ( ^ ) ( AFOAuthCredential *credential ))success failure:(void ( ^ ) ( NSError *error ))failure
Discussion

Creates and enqueues an AFHTTPRequestOperation to authenticate against the server with an authorization code, redirecting to a specified URI upon successful authentication.

Parameters

path

The path to be appended to the HTTP client’s base URL and used as the request URL.

code

The authorization code

success

A block object to be executed when the request operation finishes successfully. This block has no return value and takes a single argument: the OAuth credential returned by the server.

failure

A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a single argument: the error returned from the server.

redirectURI

The URI to redirect to after successful authentication

Declared In

AFOAuth2Client.h

authenticateUsingOAuthWithPath:parameters:success:failure:

- (void)authenticateUsingOAuthWithPath:(NSString *)path parameters:(NSDictionary *)parameters success:(void ( ^ ) ( AFOAuthCredential *credential ))success failure:(void ( ^ ) ( NSError *error ))failure
Discussion

Creates and enqueues an AFHTTPRequestOperation to authenticate against the server with the specified parameters.

Parameters

path

The path to be appended to the HTTP client’s base URL and used as the request URL.

parameters

The parameters to be encoded and set in the request HTTP body.

success

A block object to be executed when the request operation finishes successfully. This block has no return value and takes a single argument: the OAuth credential returned by the server.

failure

A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a single argument: the error returned from the server.

Declared In

AFOAuth2Client.h

authenticateUsingOAuthWithPath:refreshToken:success:failure:

- (void)authenticateUsingOAuthWithPath:(NSString *)path refreshToken:(NSString *)refreshToken success:(void ( ^ ) ( AFOAuthCredential *credential ))success failure:(void ( ^ ) ( NSError *error ))failure
Discussion

Creates and enqueues an AFHTTPRequestOperation to authenticate against the server using the specified refresh token.

Parameters

path

The path to be appended to the HTTP client’s base URL and used as the request URL.

refreshToken

The OAuth refresh token

success

A block object to be executed when the request operation finishes successfully. This block has no return value and takes a single argument: the OAuth credential returned by the server.

failure

A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a single argument: the error returned from the server.

Declared In

AFOAuth2Client.h

authenticateUsingOAuthWithPath:scope:success:failure:

- (void)authenticateUsingOAuthWithPath:(NSString *)path scope:(NSString *)scope success:(void ( ^ ) ( AFOAuthCredential *credential ))success failure:(void ( ^ ) ( NSError *error ))failure
Discussion

Creates and enqueues an AFHTTPRequestOperation to authenticate against the server with a designated scope.

Parameters

path

The path to be appended to the HTTP client’s base URL and used as the request URL.

scope

The authorization scope

success

A block object to be executed when the request operation finishes successfully. This block has no return value and takes a single argument: the OAuth credential returned by the server.

failure

A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a single argument: the error returned from the server.

Declared In

AFOAuth2Client.h

authenticateUsingOAuthWithPath:username:password:scope:success:failure:

- (void)authenticateUsingOAuthWithPath:(NSString *)path username:(NSString *)username password:(NSString *)password scope:(NSString *)scope success:(void ( ^ ) ( AFOAuthCredential *credential ))success failure:(void ( ^ ) ( NSError *error ))failure
Discussion

Creates and enqueues an AFHTTPRequestOperation to authenticate against the server using a specified username and password, with a designated scope.

Parameters

path

The path to be appended to the HTTP client’s base URL and used as the request URL.

username

The username used for authentication

password

The password used for authentication

scope

The authorization scope

success

A block object to be executed when the request operation finishes successfully. This block has no return value and takes a single argument: the OAuth credential returned by the server.

failure

A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a single argument: the error returned from the server.

Declared In

AFOAuth2Client.h

initWithBaseURL:clientID:secret:

- (id)initWithBaseURL:(NSURL *)url clientID:(NSString *)clientID secret:(NSString *)secret
Discussion

Initializes an AFOAuth2Client object with the specified base URL, client identifier, and secret.

Parameters

url

The base URL for the HTTP client. This argument must not be nil.

clientID

The client identifier issued by the authorization server, uniquely representing the registration information provided by the client.

secret

The client secret.

Return Value

The newly-initialized OAuth 2 client

Declared In

AFOAuth2Client.h

setAuthorizationHeaderWithCredential:

- (void)setAuthorizationHeaderWithCredential:(AFOAuthCredential *)credential
Discussion

Sets the “Authorization” HTTP header set in request objects made by the HTTP client to a basic authentication value with Base64-encoded username and password. This overwrites any existing value for this header.

Parameters

credential

The OAuth credential

Declared In

AFOAuth2Client.h