Skip to content

Commit

Permalink
Add reddit service to OAuthSwift demo
Browse files Browse the repository at this point in the history
  • Loading branch information
mrnguuyen committed Jan 2, 2021
1 parent efa004e commit 6dfbb74
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Demo/Common/Services.plist
Original file line number Diff line number Diff line change
Expand Up @@ -247,5 +247,12 @@
<key>consumerKey</key>
<string></string>
</dict>
<key>Reddit</key>
<dict>
<key>consumerKey</key>
<string></string>
<key>consumerSecret</key>
<string></string>
</dict>
</dict>
</plist>
44 changes: 44 additions & 0 deletions Demo/Common/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ extension ViewController {
doOAuthLyft(parameters)
case "Twitch":
doOAuthTwitch(parameters)
case "Reddit":
doOauthReddit(parameters)
default:
print("\(service) not implemented")
}
Expand Down Expand Up @@ -1390,6 +1392,48 @@ extension ViewController {
}
}
}

func doOauthReddit(_ serviceParameters: [String:String]) {
let oauthswift = OAuth2Swift(
consumerKey: serviceParameters["consumerKey"]!,
consumerSecret: serviceParameters["consumerSecret"]!,
authorizeUrl: "https://www.reddit.com/api/v1/authorize.compact",
accessTokenUrl: "https://www.reddit.com/api/v1/access_token",
responseType: "code",
contentType: "application/json"
)
self.oauthswift = oauthswift
oauthswift.accessTokenBasicAuthentification = true
oauthswift.authorizeURLHandler = getURLHandler()
let state = generateState(withLength: 20)
let _ = oauthswift.authorize(
withCallbackURL: URL(string: "oauth-swift://oauth-callback/reddit")!, scope: "read", state: state) { result in
switch result {
case .success(let (credential, _, _)):
self.showTokenAlert(name: serviceParameters["name"], credential: credential)
self.testReddit(oauthswift, credential.oauthToken)
case .failure(let error):
print(error.description)
}
}
}

func testReddit(_ oauthswift: OAuth2Swift, _ oauthToken: String) {
let applicationName = "" // Provided by developer
let username = "" // Provided by developer
let userAgent = "ios:\(applicationName):1.0 (by /u/\(username)"
let headers = ["Authorization": "Bearer \(oauthToken)", "User-Agent": userAgent]
let _ = oauthswift.client.get(
"https://oauth.reddit.com/r/all", headers: headers) { result in
switch result {
case .success(let response):
let dataString = response.string!
print(dataString)
case .failure(let error):
print(error)
}
}
}
}

let services = Services()
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Swift based OAuth library for iOS and macOS.

## Support OAuth1.0, OAuth2.0

Twitter, Flickr, Github, Instagram, Foursquare, Fitbit, Withings, Linkedin, Dropbox, Dribbble, Salesforce, BitBucket, GoogleDrive, Smugmug, Intuit, Zaim, Tumblr, Slack, Uber, Gitter, Facebook, Spotify, Typetalk, SoundCloud, Twitch, etc
Twitter, Flickr, Github, Instagram, Foursquare, Fitbit, Withings, Linkedin, Dropbox, Dribbble, Salesforce, BitBucket, GoogleDrive, Smugmug, Intuit, Zaim, Tumblr, Slack, Uber, Gitter, Facebook, Spotify, Typetalk, SoundCloud, Twitch, Reddit, etc

## Installation

Expand Down Expand Up @@ -293,6 +293,7 @@ See more examples in the demo application: [ViewController.swift](/Demo/Common/V
* [SoundCloud](https://developers.soundcloud.com/docs/api/guide#authentication)
* [Doper](https://doper.io/developer/oauth)
* [NounProject](http://api.thenounproject.com/getting_started.html#authentication)
* [Reddit](https://github.com/reddit-archive/reddit/wiki/oauth2)

## Images

Expand Down

0 comments on commit 6dfbb74

Please sign in to comment.