Skip to content

Commit d1b9eda

Browse files
committed
Merge pull request #160 from tomhoag/readmeAdditions
README.md -- Added additional instruction and code examples
2 parents 378a720 + 7d22d0d commit d1b9eda

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,19 @@ The best place to configure your client is `+[UIApplicationDelegate initialize]`
8080

8181
Take a look at the [Wiki](https://github.com/nxtbgthng/OAuth2Client/wiki) for some examples.
8282

83+
Unless otherwise specficied, the token request will be called with a HTTP Header 'Content-Type' set to 'multipart/form-data'. If you wish that header to be set to 'application/x-www-form-urlencoded', the custom header fields must be modified.
84+
85+
<pre>
86+
NSMutableDictionary *configuration = [NSMutableDictionary dictionaryWithDictionary:[[NXOAuth2AccountStore sharedStore] configurationForAccountType:kOAuth2AccountType]];
87+
NSDictionary *customHeaderFields = [NSDictionary dictionaryWithObject:@"application/x-www-form-urlencoded" forKey:@"Content-Type"];
88+
[configuration setObject:customHeaderFields forKey:kNXOAuth2AccountStoreConfigurationCustomHeaderFields];
89+
[[NXOAuth2AccountStore sharedStore] setConfiguration:configuration forAccountType:kOAuth2AccountType];
90+
</pre>
91+
92+
Consult the documentation of the OAuth2 provider to determine acceptable Content-Type header values.
93+
94+
95+
8396
### Requesting Access to a Service
8497

8598
Once you have configured your client you are ready to request access to one of those services. The NXOAuth2AccountStore provides three different methods for this:
@@ -106,6 +119,24 @@ Once you have configured your client you are ready to request access to one of t
106119
}];
107120
</pre>
108121
Using an authorization URL handler gives you the ability to open the URL in an own web view or do some fancy stuff for authentication. Therefore you pass a block to the NXOAuth2AccountStore while requesting access.
122+
123+
One method for receiving a code and exchanging it for an auth token requires the following:
124+
125+
1) Load the preparedURL into an existing UIWebView as part of the block code above. Be certain to set the delegate for the UIWebView.
126+
127+
<pre>
128+
[_webView loadRequest:[NSURLRequest requestWithURL:preparedURL]];
129+
</pre>
130+
131+
2) In the `webViewDidFinishLoad:` delegate method, you will need to parse the URL for your callback URL. If there is a match, pass that URL to `handleRedirectURL:`
132+
133+
<pre>
134+
if ([webView.request.URL.absoluteString rangeOfString:kOAuth2RedirectURL options:NSCaseInsensitiveSearch].location != NSNotFound) {
135+
[[NXOAuth2AccountStore sharedStore] handleRedirectURL:[NSURL URLWithString:webView.request.URL.absoluteString]];
136+
}
137+
</pre>
138+
139+
This is a very basic example. In the above, it is assumed that the `code` being returned from the OAuth2 provider is in the query parameter of the webView.request.URL (i.e. `http://myredirecturl.com?code=<verylongcodestring>`). This is not always the case and you may have to look elsewhere for the code (e.g. in the page content, web page title). You must prepare a URL in the format described above to pass to `handleRedirectURL:`.
109140

110141
#### On Success
111142

0 commit comments

Comments
 (0)