authentication - Instagram Login & Access Token - Swift -
i trying create "connect instagram" button allow me current user's instagram id. using these tutorials: [1] - [2], managed same twitter couldn't convert instagram. here code:
user.swift -------------- import uikit var _currentinstagramuser: instagramuser? let currentinstagramuserkey = "kcurrentinstagramuserkey" class instagramuser: nsobject { var instagramid: string? var instagramusername: string? var dictionary: nsdictionary init(dictionary: nsdictionary) { self.dictionary = dictionary instagramid = dictionary["id"] as? string instagramusername = dictionary["username"] as? string } class var currentinstagramuser: instagramuser? { { if _currentinstagramuser == nil { var data = nsuserdefaults.standarduserdefaults().objectforkey(currentinstagramuserkey) as? nsdata if data != nil { var dictionary = nsjsonserialization.jsonobjectwithdata(data!, options: nil, error: nil) as! nsdictionary _currentinstagramuser = instagramuser(dictionary: dictionary) } } return _currentinstagramuser } set(user) { _currentinstagramuser = user println(user) //save user database &&& remember user if _currentinstagramuser != nil { var data = nsjsonserialization.datawithjsonobject(user!.dictionary, options: nil, error: nil) nsuserdefaults.standarduserdefaults().setobject(data, forkey: currentinstagramuserkey) nsuserdefaults.standarduserdefaults().synchronize() } else { nsuserdefaults.standarduserdefaults().setobject(nil, forkey: currentinstagramuserkey) } nsuserdefaults.standarduserdefaults().synchronize() } } } instagramclient.swift ----------------------- let instagramconsumerkey = "###" let instagramconsumersecret = "###" let instagrambaseurl = nsurl(string: "https://api.instagram.com") class instagramclient: bdboauth1requestoperationmanager { var instagramlogincompletion: ((user: instagramuser?, error: nserror?) -> ())? class var sharedinstance: instagramclient { struct static { static let instance = instagramclient(baseurl: instagrambaseurl, consumerkey: instagramconsumerkey, consumersecret: instagramconsumersecret) } return static.instance } func loginwithcompletionforinstagram(completion: (user: instagramuser?, error: nserror?) -> ()) { instagramlogincompletion = completion //fetch request token & redirect authorization page of twitter instagramclient.sharedinstance.requestserializer.removeaccesstoken() instagramclient.sharedinstance.fetchrequesttokenwithpath("oauth/access_token", method: "post", callbackurl: nsurl(string: "tatchinsta://oauth"), scope: nil, success: { (requesttoken: bdboauth1credential!) -> void in println("success") var authurl = nsurl(string: "https://instagram.com/oauth/authorize/?client_id="###"&redirect_uri="###"&response_type=\(requesttoken.token)") uiapplication.sharedapplication().openurl(authurl!) }){ (error: nserror!) -> void in println(error) self.instagramlogincompletion?(user: nil, error: error) } } func instagramopenurl(url: nsurl) { fetchaccesstokenwithpath("oauth/access_token", method: "post", requesttoken: bdboauth1credential(querystring: url.query) , success: { (accesstoken: bdboauth1credential!) -> void in println("got access token") instagramclient.sharedinstance.requestserializer.saveaccesstoken(accesstoken) instagramclient.sharedinstance.get("https://api.instagram.com/v1/users/self/?access_token=access-token", parameters: nil, success: { (operation: afhttprequestoperation!, response:anyobject!) -> void in println("user: \(response)") var user = instagramuser(dictionary: response as! nsdictionary) instagramuser.currentinstagramuser = user self.instagramlogincompletion?(user: user, error: nil) }, failure: { (operation:afhttprequestoperation!, error:nserror!) -> void in println("error") self.instagramlogincompletion?(user: nil, error: error) }) }) { (error: nserror!) -> void in println("failed receive access token") self.instagramlogincompletion?(user: nil, error: error) } } } appdelegate.swift ------------------ func application(application: uiapplication, openurl url: nsurl, sourceapplication: string?, annotation: anyobject?) -> bool { instagramclient.sharedinstance.instagramopenurl(url) return true } viewcontroller.swift -------------------- import uikit class viewcontroller: uiviewcontroller { override func viewdidload() { super.viewdidload() } @ibaction func instagramloginbutton(sender: anyobject) { instagramclient.sharedinstance.loginwithcompletionforinstagram() { (user: instagramuser?, error: nserror?) in if user != nil { //user exists } else { //login error } } } when click on "login instagram" button (so loginwithcompletionforinstagram) function, error log receive is:
error domain=com.alamofire.error.serialization.response code=-1011 "request failed: bad request (400)" userinfo=0x7f96bb900150 {com.alamofire.serialization.response.error.response=<nshttpurlresponse: 0x7f96b9442ed0> { url: https://api.instagram.com/oauth/access_token } { status code: 400, headers { "cache-control" = "private, no-cache, no-store, must-revalidate"; connection = "keep-alive"; "content-language" = en; "content-length" = 94; "content-type" = "application/json"; date = "fri, 24 apr 2015 23:55:20 gmt"; expires = "sat, 01 jan 2000 00:00:00 gmt"; pragma = "no-cache"; server = nginx; "set-cookie" = "csrftoken=f021070d367caacba3c1c63b38ac9394; expires=fri, 22-apr-2016 23:55:20 gmt; max-age=31449600; path=/, mid=vtrx6aaaaahqfpfksk-b-edv7c0t; expires=thu, 19-apr-2035 23:55:20 gmt; max-age=630720000; path=/"; vary = "cookie, accept-language"; } }, nserrorfailingurlkey=https://api.instagram.com/oauth/access_token, com.alamofire.serialization.response.error.data=<7b22636f 6465223a 20343030 2c202265 72726f72 5f747970 65223a20 224f4175 74684578 63657074 696f6e22 2c202265 72726f72 5f6d6573 73616765 223a2022 596f7520 6d757374 2070726f 76696465 20612063 6c69656e 745f6964 227d>, nslocalizeddescription=request failed: bad request (400)}
Comments
Post a Comment