
Instagram Private API Swift
कृपया ध्यान दें SwiftyInsta अब सक्रिय रूप से अनुरक्षित नहीं है।
अधिक जानकारी के लिए #244 देखें।
यदि आप अद्यतन विकल्पों की तलाश में हैं तो Swiftagram देखें।
Instagram डेवलपर्स के लिए दो प्रकार के API प्रदान करता है। Instagram API Platform (कार्यक्षमता में अत्यंत सीमित और बंद होने के करीब), और Instagram Graph API केवल Business और Creator खातों के लिए।
हालाँकि, Instagram ऐप्स एक तीसरे प्रकार के API पर निर्भर करते हैं, जिसे Private API या Unofficial API कहा जाता है, और SwiftyInsta उनके लिए एक iOS, macOS, tvOS और watchOS क्लाइंट है, जो पूरी तरह से Swift में लिखा गया है। आप अपने उपयोगकर्ताओं के लिए बेहतर Instagram अनुभव बनाने का प्रयास कर सकते हैं, या विभिन्न कार्यों को स्वचालित करने के लिए बॉट लिख सकते हैं।
इन Private API को किसी टोकन या ऐप पंजीकरण की आवश्यकता नहीं है, लेकिन ये बाहरी उपयोग के लिए Instagram द्वारा अधिकृत नहीं हैं। इसे अपने जोखिम पर उपयोग करें।
File/Swift Packages/Add Package Dependency… चुनें।https://github.com/TheM4hd1/SwiftyInsta.git पेस्ट करें।CocoaPods Cocoa परियोजनाओं के लिए एक निर्भरता प्रबंधक है। आप इसे निम्नलिखित कमांड से स्थापित कर सकते हैं:
$ gem install cocoapods
अपने Xcode प्रोजेक्ट में SwiftyInsta को CocoaPods का उपयोग करके एकीकृत करने के लिए, इसे अपने Podfile में निर्दिष्ट करें:
use_frameworks!
target '<Your Target Name>' do
pod 'SwiftyInsta', '~> 2.0'
end
फिर, निम्नलिखित कमांड चलाएँ:
$ pod install
SwiftyInsta CryptoSwift और keychain-swift पर निर्भर करता है।
Credentials// these need to be strong references.
self.credentials = Credentials(username: /* username */, password: /* password */, verifyBy: .text)
self.handler = APIHandler()
handler.authenticate(with: .user(credentials)) {
switch $0 {
case .success(let response, _):
print("Login successful.")
// persist cache safely in the keychain for logging in again in the future.
guard let key = response.persist() else { return print("`Authentication.Response` could not be persisted.") }
// store the `key` wherever you want, so you can access the `Authentication.Response` later.
// `UserDefaults` is just an example.
UserDefaults.standard.set(key, forKey: "current.account")
UserDefaults.standard.synchronize()
case .failure(let error):
if error.requiresInstagramCode {
/* update interface to ask for code */
} else {
/* notify the user */
}
}
}
एक बार जब उपयोगकर्ता दो-कारक प्रमाणीकरण कोड या चुनौती कोड टाइप कर लेता है, तो आप बस यह करें:
self.credentials.code = /* the code */
और पिछले authenticate(with: completionHandler:) में completionHandler स्वचालित रूप से प्रतिक्रिया को पकड़ लेगा।
LoginWebViewControllerlet login = LoginWebViewController { controller, result in
controller.dismiss(animated: true, completion: nil)
// deal with authentication response.
guard let (response, _) = try? result.get() else { return print("Login failed.") }
print("Login successful.")
// persist cache safely in the keychain for logging in again in the future.
guard let key = response.persist() else { return print("`Authentication.Response` could not be persisted.") }
// store the `key` wherever you want, so you can access the `Authentication.Response` later.
// `UserDefaults` is just an example.
UserDefaults.standard.set(key, forKey: "current.account")
UserDefaults.standard.synchronize()
}
if #available(iOS 13, *) {
present(login, animated: true, completion: nil) // just swipe down to dismiss.
} else {
present(UINavigationController(rootViewController: login), // already adds a `Cancel` button to dismiss it.
animated: true,
completion: nil)
}
या LoginWebView का उपयोग करके अपना स्वयं का कस्टम UIViewController लागू करें, और इसे .webView(/* your login web view */) का उपयोग करके APIHandler authenticate विधि में पास करें।
Authentication.Responseयदि आपने पहले से किसी उपयोगकर्ता का Authentication.Response बनाए रखा है:
// recover the `key` returned by `Authentication.Response.persist()`.
// in our example, we stored it in `UserDefaults`.
guard let key = UserDefaults.standard.string(forKey: "current.account") else { return print("`key` not found.") }
// recover the safely persisted `Authentication.Response`.
guard let cache = Authentication.Response.persisted(with: key) else { return print("`Authentication.Response` not found.") }
// log in.
let handler = APIHandler()
handler.authenticate(with: .cache(cache)) { _ in
/* do something here */
}
सभी एंडपॉइंट आपके APIHandler इंस्टेंस से आसानी से सुलभ हैं।
let handler: APIHandler = /* a valid, authenticated handler */
// for instance you can…
// …fetch your inbox.
handler.messages.inbox(with: .init(maxPagesToLoad: .max),
updateHandler: nil,
completionHandler: { _, _ in /* do something */ })
// …fetch all your followers.
handler.users.following(user: .me,
with: .init(maxPagesToLoad: .max),
updateHandler: nil,
completionHandler: { _, _ in /* do something */ })
इसके अलावा, प्रतिक्रियाएँ अब API द्वारा लौटाई गई JSON फ़ाइल में निहित प्रत्येक मान प्रदर्शित करती हैं: बस किसी भी ParsedResponse के rawResponse तक पहुँचें और ब्राउज़ करना शुरू करें, या सुझाए गए एक्सेसरीज़ (जैसे User का username, name, आदि और Media का aspectRatio, takenAt, content, आदि) के साथ रहें।
Pull requests और issues का स्वागत है।
1.* अनुरक्षक2.* अनुरक्षकहम सक्रिय रूप से अनुरक्षकों की तलाश कर रहे हैं।
अधिक जानकारी के लिए #244 देखें।
SwiftyInsta MIT लाइसेंस के तहत लाइसेंस प्राप्त है। अधिक जानकारी के लिए LICENSE देखें।