oAuth Instagram login

2019-05-01 17:00发布

问题:

I am trying to get access token from Instagram using oAuth 2, to use it in scopes. I couldn't understand how should I configure "redirect_uri" (tried a lot of combinations) if I use it in native app, it means I don't need any websites to have redirected, I need redirect to app itself, after logged in. (Maybe I explained something not correctly, because don't understand how redirect_uri works in native apps)

In AppDelegate.swift added:

func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
    if (url.host == "oauth-callback") {
        OAuthSwift.handleOpenURL(url)
    }
    return true
}

In ViewController.swift:

import UIKit
import OAuthSwift

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let oauthswift = OAuth2Swift(
            consumerKey:    "cc6dd7ca50c74f4f92a1a8328c9d4426",
            consumerSecret: "74f6bf3c839b4e15a747a332d#######",
            authorizeUrl:   "https://api.instagram.com/oauth/authorize",
            responseType:   "token"
        )
        oauthswift.authorizeWithCallbackURL(
            NSURL(string: "oauth-swift://oauth-callback/instagram")!,
            scope: "likes+comments", state:"INSTAGRAM",
            success: { credential, response, parameters in
                print(credential.oauth_token)
            },
            failure: { error in
                print(error.localizedDescription)
            }
        )

    }
}

(can't add image without url because of reputation) :

Url schemes in project info

Instagram settings

Question:

  • What I am doing wrong?

  • How to fix it so app can "print(credential.oauth_token)" happen.

Fixed:

NSURL(string: "oauth-swift://oauth-callback/instagram")!,

should be changed to

NSURL(string: "AppName://oauth-callback")!,

And also in Instagram settings