Reputation: 2533
I'm trying to use Shyambhat's InstagramKit (from the DEV branch) and I just can't get the receivedValidAccessTokenFromURL
method working.
Currently I'm working with swift 3.0 the code looks as follows (for testing purposes)
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
engine = InstagramEngine.shared();
let iScope: InstagramKitLoginScope = [.comments, .followerList, .likes, .relationships];
let iAuthURL = engine.authorizationURL(for: iScope);
loginWebView.loadRequest(URLRequest(url: iAuthURL));
loginWebView.delegate = self;
}
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
let url = request.url;
try! engine.receivedValidAccessToken(from: url!)
return true;
}
I've set the following in my info.plist
InstagramAppClientId: 'myid'
InstagramAppRedirectURL: 'http://example.com'
For some reason instagram doesn't allow the redirect url to be app:// anymore. When I put a debug on the request.url. This shows me the following
https://api.instagram.com/oauth/authorize/?client_id=myid&redirect_uri=http%3A//example.com&response_type=token&scope=comments%20relationships%20likes%20follower_list
I'm hoping someone here has experience with this library and can help me out.
Upvotes: 2
Views: 588
Reputation: 2533
I've figured it out. What I did is the following.
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
do {
if let url = request.url {
if (String(describing: url).range(of: "access") != nil){
try engine.receivedValidAccessToken(from: url)
if let accessToken = engine.accessToken {
NSLog("accessToken: \(accessToken)")
}
}
}
} catch let err as NSError {
print(err.debugDescription)
}
return true
}
I check if the url contains the access token if it does then I'll let the engine actually get the access token. The error occurred because it was trying to get the access token while it wasn't available when you open the app up the first time.
Upvotes: 4
Reputation: 25294
InstagramKit Swift 3.0 get access token:
Info.plist (added)
<key>InstagramAppClientId</key>
<string>3ewqrewr23453243244qwrqwefwefgw42</string>
<key>InstagramAppRedirectURL</key>
<string>https://www.instagram.com/</string>
ViewController.swift
import UIKit
import InstagramKit
class ViewController: UIViewController {
var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// deleteChache()
initWebView()
}
func deleteChache() {
URLCache.shared.removeAllCachedResponses()
if let cookies = HTTPCookieStorage.shared.cookies {
for cookie in cookies {
HTTPCookieStorage.shared.deleteCookie(cookie)
}
}
}
}
// MARK: UIWebView
extension ViewController: UIWebViewDelegate {
func initWebView() {
webView = UIWebView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
webView.delegate = self
view.addSubview(webView)
authorizationRequestInWebView()
}
func authorizationRequestInWebView() {
let url = InstagramEngine.shared().authorizationURL()
let request = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 10.0)
webView.loadRequest(request)
}
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
do {
if let url = request.url {
try InstagramEngine.shared().receivedValidAccessToken(from: url)
if let accessToken = InstagramEngine.shared().accessToken {
NSLog("accessToken: \(accessToken)")
}
}
} catch let err as NSError {
print(err.debugDescription)
}
return true
}
}
Upvotes: 1