InstagramKit receivedValidAccessTokenFromURL 不起作用

InstagramKit receivedValidAccessTokenFromURL doesn't work

我正在尝试使用 Shyambhat 的 InstagramKit(来自 DEV 分支),但我无法使用 receivedValidAccessTokenFromURL 方法。

目前我正在使用 swift 3.0 代码如下(用于测试目的)

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;
    }

我在 info.plist

中设置了以下内容

InstagramAppClientId: 'myid'

InstagramAppRedirectURL: 'http://example.com'

出于某种原因,instagram 不再允许重定向 url 为 app://。 当我在 request.url 上进行调试时。这向我展示了以下内容

https://api.instagram.com/oauth/authorize/?client_id=myid&redirect_uri=http%3A//example.com&response_type=token&scope=comments%20relationships%20likes%20follower_list

我希望这里有人对这个库有经验,可以帮助我。

InstagramKit Swift 3.0 获取访问令牌:

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
}
}

控制台结果

我已经弄明白了。我所做的是以下内容。

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
    }

我检查 url 是否包含访问令牌,如果包含,那么我会让引擎实际获取访问令牌。发生错误是因为它试图获取访问令牌,而当您第一次打开应用程序时它不可用。