Oauth2, Swift 3, Instagram

OAuth2, Swift 3, Instagram

For Swift 3:

Update: April 17 2017: Because of broken dependencies, the installation by Pods is no longer working. Therefore I have ripped the needed content and created a new Github project using a Bridging Header and stored all the needed files within the project. If you clone or download the github project, you'll instantly be able to login to Instagram.

To use those files in your project, simply drag and drop all the files from the SimpleAuth folder to your project, make sure to mark copy item if needed

Sample Image

Also you need to disable Disable implicit oAuth within the Instagram developer console.

Sample Image

Then you either copy/paste my code from the Bridging Header into your or you use mine. Set the Bridging Header at the Target's Build Settings.

Sample Image

Everything else works as before:

I have a struct for the Instagram Account:

struct InstagramUser {

var token: String = ""
var uid: String = ""
var bio: String = ""
var followed_by: String = ""
var follows: String = ""
var media: String = ""
var username: String = ""
var image: String = ""
}

The function to receive the token:

typealias JSONDictionary = [String:Any]
var user: InstagramUser?
let INSTAGRAM_CLIENT_ID = "16ee14XXXXXXXXXXXXXXXXXXXXXXXXX"
let INSTAGRAM_REDIRECT_URI = "http://www.davidseek.com/just_a_made_up_dummy_url" //just important, that it matches your developer account uri at Instagram

extension ViewController {

func connectInstagram() {

let auth: NSMutableDictionary = ["client_id": INSTAGRAM_CLIENT_ID,
SimpleAuthRedirectURIKey: INSTAGRAM_REDIRECT_URI]

SimpleAuth.configuration()["instagram"] = auth
SimpleAuth.authorize("instagram", options: [:]) { (result: Any?, error: Error?) -> Void in

if let result = result as? JSONDictionary {

var token = ""
var uid = ""
var bio = ""
var followed_by = ""
var follows = ""
var media = ""
var username = ""
var image = ""

token = (result["credentials"] as! JSONDictionary)["token"] as! String
uid = result["uid"] as! String

if let extra = result["extra"] as? JSONDictionary,
let rawInfo = extra ["raw_info"] as? JSONDictionary,
let data = rawInfo["data"] as? JSONDictionary {

bio = data["bio"] as! String

if let counts = data["counts"] as? JSONDictionary {
followed_by = String(describing: counts["followed_by"]!)
follows = String(describing: counts["follows"]!)
media = String(describing: counts["media"]!)
}
}

if let userInfo = result["user_info"] as? JSONDictionary {
username = userInfo["username"] as! String
image = userInfo["image"] as! String
}

self.user = InstagramUser(token: token, uid: uid, bio: bio, followed_by: followed_by, follows: follows, media: media, username: username, image: image)

} else {
// this handles if user aborts or the API has a problem like server issue
let alert = UIAlertController(title: "Error!", message: nil, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}

if error != nil {
print("Error during SimpleAuth.authorize: \(error)")
}
}
}
}

Instagram also says:

Important

Even though our access tokens do not specify an expiration time, your
app should handle the case that either the user revokes access, or
Instagram expires the token after some period of time. If the token is
no longer valid, API responses will contain an
“error_type=OAuthAccessTokenException”. In this case you will need to
re-authenticate the user to obtain a new valid token. In other words:
do not assume your access_token is valid forever.

So handle the case of receiving the OAuthAccessTokenException

Swift / Instagram API - how to auth with Instagram App

No, you cannot use Instagram app to authenticate Instagram account at the moment.

However, web view seems to be a reasonable way to login especially in iOS 9 and above because it does not require user to switch between apps and having to tap on dialogs to switch app. The only pain is for the user to type username and password, but hopefully they only need to do that once (unless they change account).

Facebook has similar issue and choose to go with web view login as default way to authenticate.

For the people who are not signed into Facebook on Safari, they will only need to log into Facebook one time. After that, every future Facebook Login experience is fast and convenient with no extra steps. This means that as adoption of this flow increases over time, so does the quality of the experience people get. In contrast, the traditional fast-app-switch flow does not improve over time: The additional dialogs continue to appear in both directions for every new app that people log into.

https://developers.facebook.com/blog/post/2015/10/29/Facebook-Login-iOS9/

Enter a valid website when entering Redirect URI in instagram in ios

Doing many hours of research I finally find solution from this link.
OAuth2, Swift 3, Instagram

We can integrate instagram with SimpleAuth library as mentioned there.

I hope this will help others.

Questions on Instagram API changes for use with Swift 3. Latitude and Longitude, search by #hastag

In Instagram sandbox mode, you can only access up to 10 authorized sandbox users. To add authorized sandbox users, you'll have to invite them to your app and they will have to accept the invitation.

You can always request any endpoint when using this sandbox mode, but it will only resulted in 20 recent media of each authorized sandbox users. That is also the reason why you can only access your 20 last images.

I can't answer for your disappearance of lat and long data. It should give the data nonetheless.

source: https://www.instagram.com/developer/sandbox/

How to get access token from Instagram API

I found the solution to my own question here Impossible to get an access_token for Instagram Basic Display API

When using Postman for Instagram's API, you should fill out the info required under the POST request's Body, NOT the Parameters. And when in the body, just select x-www-form-irlencoded



Related Topics



Leave a reply



Submit