在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:OAuthSwift/OAuthSwift开源软件地址:https://github.com/OAuthSwift/OAuthSwift开源编程语言:Swift 98.3%开源软件介绍:OAuthSwiftSwift based OAuth library for iOS and macOS. Support OAuth1.0, OAuth2.0Twitter, 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 InstallationOAuthSwift is packaged as a Swift framework. Currently this is the simplest way to add it to your app:
Support Carthage
Support CocoaPods
platform :ios, '10.0'
use_frameworks!
pod 'OAuthSwift', '~> 2.2.0' Swift Package Manager Supportimport PackageDescription
let package = Package(
name: "MyApp",
dependencies: [
.package(name: "OAuthSwift",
url: "https://github.com/OAuthSwift/OAuthSwift.git",
.upToNextMajor(from: "2.2.0"))
]
) Old versionsSwift 3Use the Swift 4Use the tag Objective-CUse the tag How toSetting URL SchemesIn info tab of your target Replace oauth-swift by your application name Handle URL in AppDelegate
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
if url.host == "oauth-callback" {
OAuthSwift.handle(url: url)
}
return true
}
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
guard let url = URLContexts.first?.url else {
return
}
if url.host == "oauth-callback" {
OAuthSwift.handle(url: url)
}
} if options[.sourceApplication] as? String == "com.apple.SafariViewService" {
func applicationDidFinishLaunching(_ aNotification: NSNotification) {
NSAppleEventManager.shared().setEventHandler(self, andSelector:#selector(AppDelegate.handleGetURL(event:withReplyEvent:)), forEventClass: AEEventClass(kInternetEventClass), andEventID: AEEventID(kAEGetURL))
}
func handleGetURL(event: NSAppleEventDescriptor!, withReplyEvent: NSAppleEventDescriptor!) {
if let urlString = event.paramDescriptor(forKeyword: AEKeyword(keyDirectObject))?.stringValue, let url = URL(string: urlString) {
OAuthSwift.handle(url: url)
}
} Authorize with OAuth1.0// create an instance and retain it
oauthswift = OAuth1Swift(
consumerKey: "********",
consumerSecret: "********",
requestTokenUrl: "https://api.twitter.com/oauth/request_token",
authorizeUrl: "https://api.twitter.com/oauth/authorize",
accessTokenUrl: "https://api.twitter.com/oauth/access_token"
)
// authorize
let handle = oauthswift.authorize(
withCallbackURL: "oauth-swift://oauth-callback/twitter") { result in
switch result {
case .success(let (credential, response, parameters)):
print(credential.oauthToken)
print(credential.oauthTokenSecret)
print(parameters["user_id"])
// Do your request
case .failure(let error):
print(error.localizedDescription)
}
} OAuth1 without authorizationNo urls to specify here // create an instance and retain it
oauthswift = OAuth1Swift(
consumerKey: "********",
consumerSecret: "********"
)
// do your HTTP request without authorize
oauthswift.client.get("https://api.example.com/foo/bar") { result in
switch result {
case .success(let response):
//....
case .failure(let error):
//...
}
} Authorize with OAuth2.0// create an instance and retain it
oauthswift = OAuth2Swift(
consumerKey: "********",
consumerSecret: "********",
authorizeUrl: "https://api.instagram.com/oauth/authorize",
responseType: "token"
)
let handle = oauthswift.authorize(
withCallbackURL: "oauth-swift://oauth-callback/instagram",
scope: "likes+comments", state:"INSTAGRAM") { result in
switch result {
case .success(let (credential, response, parameters)):
print(credential.oauthToken)
// Do your request
case .failure(let error):
print(error.localizedDescription)
}
} Authorize with OAuth2.0 and proof key flow (PKCE)// create an instance and retain it
oauthswift = OAuth2Swift(
consumerKey: "********",
consumerSecret: "********",
authorizeUrl: "https://server.com/oauth/authorize",
responseType: "code"
)
oauthswift.accessTokenBasicAuthentification = true
guard let codeVerifier = generateCodeVerifier() else {return}
guard let codeChallenge = generateCodeChallenge(codeVerifier: codeVerifier) else {return}
let handle = oauthswift.authorize(
withCallbackURL: "myApp://callback/",
scope: "requestedScope",
state:"State01",
codeChallenge: codeChallenge,
codeChallengeMethod: "S256",
codeVerifier: codeVerifier) { result in
switch result {
case .success(let (credential, response, parameters)):
print(credential.oauthToken)
// Do your request
case .failure(let error):
print(error.localizedDescription)
}
} See demo for more examples Handle authorize URLThe authorize URL allows the user to connect to a provider and give access to your application. By default this URL is opened into the external web browser (ie. safari), but apple does not allow it for app-store iOS applications. To change this behavior you must set an oauthswift.authorizeURLHandler = .. For instance you can embed a web view into your application by providing a controller that displays a web view ( func handle(_ url: NSURL) {
let req = URLRequest(URL: targetURL)
self.webView.loadRequest(req)
... and present the view ( Use the SFSafariViewController (iOS9)A default implementation of oauthswift.authorizeURLHandler = SafariURLHandler(viewController: self, oauthSwift: oauthswift) Of course you can create your own class or customize the controller by setting the variable Make signed requestJust call HTTP functions of oauthswift.client.get("https://api.linkedin.com/v1/people/~") { result in
switch result {
case .success(let response):
let dataString = response.string
print(dataString)
case .failure(let error):
print(error)
}
}
// same with request method
oauthswift.client.request("https://api.linkedin.com/v1/people/~", .GET,
parameters: [:], headers: [:],
completionHandler: { ... See more examples in the demo application: ViewController.swift OAuth provider pages
ImagesContributingSee CONTRIBUTING.md IntegrationOAuthSwift could be used with others frameworks You can sign Alamofire request with OAuthSwiftAlamofire To achieve great asynchronous code you can use one of these integration frameworks LicenseOAuthSwift is available under the MIT license. See the LICENSE file for more info. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论