Hussam Abedlatif
Hussam Abedlatif

Reputation: 141

Instagram IOS API Implementation

I am trying to get the authentication token to be able to grab a user's info. I registered my app in the instagram api page and everything seems to work except that I am not able to retrieve an authentication token or anything information. (I think it might be because of the redirect url i just made a dummy url) I can login to my instagram account and authorize my app to retrieve information but I dont get anything printed on my console so Im assuming im not being able to retrieve anything.

the code:

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

Upvotes: 0

Views: 2063

Answers (3)

LoraKucher
LoraKucher

Reputation: 83

U have used WKWebView and UIWebViewDelegate. It is not WKWebView delegate. This view has been inherited from UIView - not from uiwebview - that's why delegates methods does not work. Try to use WKNavigationDelegate with its methods.

Upvotes: 1

Kalaivani
Kalaivani

Reputation: 424

We need "Access token" to go further.But it seems empty in your case.Try to run local server either through node/jupiter/MAMP or anything and check your localhost page is popping up.

Once your localhost is up and running.Please paste the below link in the browser by replacing the client_id to yours. https://www.instagram.com/oauth/authorize/?client_id=Your_Client_Id&redirect_uri=http://localhost:8000&response_type=token&scope=public_content

Make sure you are giving the same redirect uri while you registered. Please follow this link for further clarification

Click on Authorize in the page to be displayed in the browser.And check the URL in the browser, your access token would be passed for the next page.Copy this token and use it in the code

Upvotes: 0

Jay
Jay

Reputation: 2661

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
}

Upvotes: 0

Related Questions