【问题标题】:Instagram IOS API ImplementationInstagram IOS API 实现
【发布时间】:2018-01-24 21:39:39
【问题描述】:

我正在尝试获取身份验证令牌以获取用户信息。我在 instagram api 页面中注册了我的应用程序,除了我无法检索身份验证令牌或任何信息外,一切似乎都正常。 (我认为这可能是因为我刚刚创建了一个虚拟 url 的重定向 url)我可以登录到我的 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/ */

}

enter image description here

【问题讨论】:

    标签: ios swift instagram-api


    【解决方案1】:

    你使用过 WKWebView 和 UIWebViewDelegate。它不是 WKWebView 委托。这个视图是从 UIView 继承的——不是从 uiwebview 继承的——这就是委托方法不起作用的原因。尝试使用 WKNavigationDelegate 及其方法。

    【讨论】:

      【解决方案2】:
      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
      }
      

      【讨论】:

        【解决方案3】:

        我们需要“访问令牌”才能走得更远。但在你的情况下它似乎是空的。尝试通过 node/jupiter/MAMP 或任何东西运行本地服务器,并检查你的 localhost 页面是否弹出。

        一旦您的 localhost 启动并运行。请将以下链接粘贴到浏览器中,将 client_id 替换为您的。 https://www.instagram.com/oauth/authorize/?client_id=Your_Client_Id&redirect_uri=http://localhost:8000&response_type=token&scope=public_content

        确保您在注册时提供相同的重定向 uri。 请关注this链接以获得进一步说明

        点击要在浏览器中显示的页面中的Authorize。并检查浏览器中的URL,您的访问令牌将被传递给下一页。复制此令牌并在代码中使用它

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-04-26
          • 2015-08-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多