簡體   English   中英

Instagram IOS API實現

[英]Instagram IOS API Implementation

我正在嘗試獲取身份驗證令牌,以便能夠獲取用戶的信息。 我在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/ */

}

在此處輸入圖片說明

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
}

我們需要“訪問令牌”來做進一步的工作。但是,在您的情況下,它似乎是空的。嘗試通過node / jupiter / MAMP或其他任何方式運行本地服務器,並檢查您的本地主機頁面是否彈出。

一旦您的本地主機啟動並運行,請將下面的鏈接替換為您的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。 請點擊鏈接以進一步說明

在頁面上單擊授權以顯示在瀏覽器中。然后在瀏覽器中檢查URL,您的訪問令牌將被傳遞到下一頁。復制此令牌並在代碼中使用它

U已使用WKWebView和UIWebViewDelegate。 它不是WKWebView委托。 該視圖是從UIView繼承的,而不是從uiwebview繼承的,這就是委托方法不起作用的原因。 嘗試使用WKNavigationDelegate及其方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM