InstagramKit receivedValidAccessTokenFromURL不起作用

时间:2016-09-27 22:27:21

标签: ios swift instagram instagram-api

我正在尝试使用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不允许重定向网址为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

我希望这里有人有这个图书馆的经验,可以帮助我。

2 个答案:

答案 0 :(得分:4)

我已经弄清楚了。我做的是以下内容。

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是否包含访问令牌,如果确实如此,我将让引擎实际获得访问令牌。发生错误的原因是,当您第一次打开应用程序时,它正在尝试获取访问令牌,但它不可用。

答案 1 :(得分:1)

InstagramKit Swift 3.0获取访问令牌:

  

Info.plist(已添加)

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

控制台结果