我正在尝试获取身份验证令牌以获取用户的信息。我在instagram api页面注册了我的应用程序,除了我无法检索身份验证令牌或任何信息外,一切似乎都有效。 (我认为这可能是因为重定向网址我只是制作了一个虚拟网址)我可以登录我的Instagram帐户并授权我的应用程序检索信息,但我没有在我的控制台上打印任何东西所以我假设我无法检索任何东西。
代码:
import UIKit
import WebKit
class ViewController3: UIViewController, UIWebViewDelegate {
@IBOutlet weak var WebView1: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let authURL = String(format: "%@?client_id=%@&redirect_uri=%@&response_type=token&scope=%@&DEBUG=True", arguments: [API.INSTAGRAM_AUTHURL,API.INSTAGRAM_CLIENT_ID,API.INSTAGRAM_REDIRECT_URI, API.INSTAGRAM_SCOPE])
let urlRequest = URLRequest.init(url: URL.init(string: authURL)!)
WebView1.load(urlRequest)
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
var tts = segue.destination as! Manage_Ad_VC
tts.S_Media_R = "Instagram"
}
func WebView1(_ WebView1: UIWebView, shouldStartLoadWith request:URLRequest, navigationType: UIWebViewNavigationType) -> Bool{
return checkRequestForCallbackURL(request: request)
}
func checkRequestForCallbackURL(request: URLRequest) -> Bool {
print("Instagram authentication token ==")
let requestURLString = (request.url?.absoluteString)! as String
if requestURLString.hasPrefix(API.INSTAGRAM_REDIRECT_URI) {
let range: Range<String.Index> = requestURLString.range(of: "#access_token=")!
handleAuth(authToken: requestURLString.substring(from: range.upperBound))
return false;
}
return true
}
func handleAuth(authToken: String) {
print("Instagram authentication token ==", authToken)
}
}
struct API {
static let INSTAGRAM_AUTHURL = "https://api.instagram.com/oauth/authorize/"
static let INSTAGRAM_CLIENT_ID = "myclientidgoeshere"
static let INSTAGRAM_CLIENTSERCRET = " myclientsercretgoeshere "
static let INSTAGRAM_REDIRECT_URI = "http://www.dummyurl.com/just_a_made_up_dummy_url"
static let INSTAGRAM_ACCESS_TOKEN = ""
static let INSTAGRAM_SCOPE = "follower_list+public_content" /* add whatever scope you need https://www.instagram.com/developer/authorization/ */
}
答案 0 :(得分:0)
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if let url = request.url, url.host == "URL FOR OAUTH GOES HERE" {
if url.absoluteString.range(of: "access_token") != nil {
let urlParts = url.absoluteString.components(separatedBy: "=")
let code = urlParts[1]
let userInfoURL = "https://api.instagram.com/v1/users/self/?access_token=" + code
//Make request with the userInfoURL to retrieve the user Info.
}
}
return true
}
答案 1 :(得分:0)
我们需要“访问令牌”来进一步。但是,在您的情况下,它似乎是空的。尝试通过node / jupiter / MAMP或其他任何方式运行本地服务器,并检查您的本地主机页面是否弹出。
一旦您的本地主机启动并运行,请通过将client_id替换为您的本地链接,将以下链接粘贴到浏览器中。 https://www.instagram.com/oauth/authorize/?client_id= 您的客户ID &redirect_uri = http://localhost:8000&response_type=token&scope=public_content
确保在注册时提供相同的重定向uri。 请点击this链接以进一步说明
在页面上单击授权以显示在浏览器中。然后在浏览器中检查URL,下一个页面将通过您的访问令牌。复制该令牌并在代码中使用它
答案 2 :(得分:0)
U使用了WKWebView和UIWebViewDelegate。它不是WKWebView委托。该视图是从UIView继承的,而不是从uiwebview继承的,这就是委托方法不起作用的原因。尝试将WKNavigationDelegate及其方法一起使用。