diff --git a/Vendoo.xcworkspace/xcuserdata/okechi.xcuserdatad/UserInterfaceState.xcuserstate b/Vendoo.xcworkspace/xcuserdata/okechi.xcuserdatad/UserInterfaceState.xcuserstate index a990e79..a160318 100644 Binary files a/Vendoo.xcworkspace/xcuserdata/okechi.xcuserdatad/UserInterfaceState.xcuserstate and b/Vendoo.xcworkspace/xcuserdata/okechi.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/Vendoo.xcworkspace/xcuserdata/okechi.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/Vendoo.xcworkspace/xcuserdata/okechi.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist index 6dc9aa6..0a85477 100644 --- a/Vendoo.xcworkspace/xcuserdata/okechi.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist +++ b/Vendoo.xcworkspace/xcuserdata/okechi.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -74,13 +74,45 @@ ignoreCount = "0" continueAfterRunningActions = "No" filePath = "Vendoo/NetworksTableViewController.swift" - timestampString = "486467691.902354" + timestampString = "486495491.668853" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "399" - endingLineNumber = "399" + startingLineNumber = "406" + endingLineNumber = "406" landmarkName = "tableView(_:didSelectRowAtIndexPath:)" landmarkType = "5"> + + + + + + @@ -106,11 +138,11 @@ ignoreCount = "0" continueAfterRunningActions = "No" filePath = "Vendoo/EtsyRESTAPIManager.swift" - timestampString = "486489301.995309" + timestampString = "486509277.070357" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "145" - endingLineNumber = "145" + startingLineNumber = "156" + endingLineNumber = "156" landmarkName = "authorizeApp(_:)" landmarkType = "5"> @@ -122,11 +154,11 @@ ignoreCount = "0" continueAfterRunningActions = "No" filePath = "Vendoo/EtsyRESTAPIManager.swift" - timestampString = "486489301.995309" + timestampString = "486509277.070357" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "147" - endingLineNumber = "147" + startingLineNumber = "158" + endingLineNumber = "158" landmarkName = "authorizeApp(_:)" landmarkType = "5"> @@ -138,7 +170,199 @@ ignoreCount = "0" continueAfterRunningActions = "No" filePath = "Vendoo/EtsyRESTAPIManager.swift" - timestampString = "486489480.473889" + timestampString = "486509277.070357" + startingColumnNumber = "9223372036854775807" + endingColumnNumber = "9223372036854775807" + startingLineNumber = "119" + endingLineNumber = "119" + landmarkName = "authorizeApp(_:)" + landmarkType = "5"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -170,14 +394,142 @@ ignoreCount = "0" continueAfterRunningActions = "No" filePath = "Vendoo/EtsyRESTAPIManager.swift" - timestampString = "486489552.994372" + timestampString = "486509277.070357" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "99" - endingLineNumber = "99" + startingLineNumber = "134" + endingLineNumber = "134" landmarkName = "authorizeApp(_:)" landmarkType = "5"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Vendoo/EtsyRESTAPIManager.swift b/Vendoo/EtsyRESTAPIManager.swift index b82e628..cea9802 100644 --- a/Vendoo/EtsyRESTAPIManager.swift +++ b/Vendoo/EtsyRESTAPIManager.swift @@ -22,6 +22,8 @@ class EtsyRESTAPIManager: NSObject { //----------------------------------------------// static let sharedInstance = EtsyRESTAPIManager() + + let baseURL = "https://openapi.etsy.com/v2/private" private var apiKey: String! @@ -32,8 +34,7 @@ class EtsyRESTAPIManager: NSObject { //User specific class variables private var etsyUser: String! - private var oauth_token: String! - private var oauth_secret: String! + private var oauthswift: OAuth1Swift! //private let oauthswift = OAuth1Swift(parameters: ["consumerKey":"snbs78qkfy3yqq6yhe6yv49b","consumerSecret":"4sbva4oqb6", "requestTokenUrl": "https://openapi.etsy.com/v2/oauth/request_token?scope=listings_w%20listings_r%20listings_d%20transactions_r%20transactions_w%20"]) override init(){ @@ -42,12 +43,16 @@ class EtsyRESTAPIManager: NSObject { self.apiKey = ((dict["Etsy"] as! Dictionary)["consumerKey"] as! String) self.apiSecret = ((dict["Etsy"] as! Dictionary)["consumerSecret"] as! String) + oauthswift = OAuth1Swift(parameters: ["consumerKey":self.apiKey,"consumerSecret":self.apiSecret, "requestTokenUrl" : "https://openapi.etsy.com/v2/oauth/request_token?scope=listings_w%20listings_r%20listings_d%20transactions_r%20transactions_w", + "authorizeUrl": "https://www.etsy.com/oauth/signin?", + "accessTokenUrl": "https://openapi.etsy.com/v2/oauth/access_token?"]) if(self.isAuthorized){ - let dictionary = Locksmith.loadDataForUserAccount(self.userEmail) + let dictionary = Locksmith.loadDataForUserAccount(self.userEmail, inService: "vendoo_oauth_etsy") let oauthDictionary = dictionary?["etsy_oauth"] as? Dictionary - self.oauth_token = oauthDictionary?["token"] as? String! - self.oauth_secret = oauthDictionary?["secret"] as? String! + print((oauthDictionary?["token"] as? String!)!) + self.oauthswift.client.credential.oauth_token = (oauthDictionary?["token"] as? String!)! + self.oauthswift.client.credential.oauth_token_secret = (oauthDictionary?["secret"] as? String!)! } @@ -65,12 +70,10 @@ extension EtsyRESTAPIManager { //makes application ready for use with users etsy account func authorizeApp(viewcontroller: UIViewController){ - + var boolResult: Bool! if(!isAuthorized){ - let oauthswift = OAuth1Swift(parameters: ["consumerKey":self.apiKey,"consumerSecret":self.apiSecret, "requestTokenUrl" : "https://openapi.etsy.com/v2/oauth/request_token?scope=listings_w%20listings_r%20listings_d%20transactions_r%20transactions_w", - "authorizeUrl": "https://www.etsy.com/oauth/signin?", - "accessTokenUrl": "https://openapi.etsy.com/v2/oauth/access_token?"]) + oauthswift!.authorize_url_handler = SafariURLHandler(viewController: viewcontroller) @@ -94,47 +97,55 @@ extension EtsyRESTAPIManager { ) dispatch_async(dispatch_get_main_queue(), {() -> Void in - oauthswift!.authorizeWithCallbackURL( NSURL(string: "vendoo://oauth-callback/etsy")!, success: { + + + self.oauthswift!.authorizeWithCallbackURL( NSURL(string: "vendoo://oauth-callback/etsy")!, success: { credential, response, parameters in print(credential.oauth_token) print(credential.oauth_token_secret) print(response) - + boolResult = false }, failure: { error in print(error) dispatch_async(dispatch_get_main_queue(), {() -> Void in - print(oauthswift!.client.credential.oauth_token) - print(oauthswift!.client.credential.oauth_token_secret) + print(self.oauthswift!.client.credential.oauth_token) + print(self.oauthswift!.client.credential.oauth_token_secret) //save data to keychain - let creds = oauthswift?.client.credential + let creds = self.oauthswift?.client.credential let oauth: Dictionary? = [ "token" : creds!.oauth_token, "secret" : creds!.oauth_token_secret ] do{ - try Locksmith.saveData(["etsy_oauth" : oauth!], forUserAccount: self.userEmail) + try Locksmith.saveData(["etsy_oauth" : oauth!], forUserAccount: self.userEmail, inService: "vendoo_oauth_etsy") print("account credentials saved") NSUserDefaults.standardUserDefaults().setBool(true, forKey: "etsyAuthorized") self.isAuthorized = true + boolResult = true + + self.oauthswift!.client.get("https://openapi.etsy.com/v2/oauth/scopes", + success: { + data, response in + let dataString = NSString(data: data, encoding: NSUTF8StringEncoding) + print(dataString) + boolResult = true + } + , failure: { error in + print(error) + boolResult = true + } + ) } catch{ (error) print(error) //could not save data to keychain print("account credentials could not be saved") + boolResult = false } - oauthswift!.client.get(self.baseURL, - success: { - data, response in - let dataString = NSString(data: data, encoding: NSUTF8StringEncoding) - print(dataString) - } - , failure: { error in - print(error) - } - ) + //return boolResult }) }) @@ -147,6 +158,7 @@ extension EtsyRESTAPIManager { + //once everything is authorized save true value to the authorization boolean /* NSUserDefaults.standardUserDefaults().setBool(true, forKey: "etsyAuthorized") @@ -155,13 +167,90 @@ extension EtsyRESTAPIManager { } - func deAuthorizeApp(viewcontroller: UIViewController) -> Bool { + func deAuthorizeApp(viewcontroller: UIViewController){ + + + + + + oauthswift!.authorize_url_handler = SafariURLHandler(viewController: viewcontroller) + + oauthswift!.client.get("https://openapi.etsy.com/v2/oauth/request_token?scope=listings_w%20listings_r%20listings_d%20transactions_r%20transactions_w", + success: { + data, response in + let dataString = NSString(data: data, encoding: NSUTF8StringEncoding) + print(dataString!) + print(response) + + + + + //getting error here when trying to retrieve the login url from the response + //var dataDictionary = self.convertStringToDictionary(dataString! as String) + //print(dataDictionary!["login_url"]) + } + , failure: { error in + print(error) + } + ) + + dispatch_async(dispatch_get_main_queue(), {() -> Void in + + + self.oauthswift!.authorizeWithCallbackURL( NSURL(string: "vendoo://oauth-callback/etsy")!, success: { + credential, response, parameters in + print(credential.oauth_token) + print(credential.oauth_token_secret) + print(response) + + //erase all authorization variables + do { + try Locksmith.deleteDataForUserAccount(self.userEmail, inService: "vendoo_oauth_etsy") + self.oauthswift.client.credential.oauth_token = "" + self.oauthswift.client.credential.oauth_token_secret = "" + NSUserDefaults.standardUserDefaults().setBool(false, forKey: "etsyAuthorized") + self.isAuthorized = false + + print("oauth credentials deleted and app deauthorized from etsy") + }catch{ + (error) + print(error) + ("failed to delete credentails and deauthorize app") + } + + + }, failure: { error in + print(error) + + dispatch_async(dispatch_get_main_queue(), {() -> Void in + + //erase all authorization variables + do { + try Locksmith.deleteDataForUserAccount(self.userEmail, inService: "vendoo_oauth_etsy") + self.oauthswift.client.credential.oauth_token = "" + self.oauthswift.client.credential.oauth_token_secret = "" + NSUserDefaults.standardUserDefaults().setBool(false, forKey: "etsyAuthorized") + self.isAuthorized = false + + print("oauth credentials deleted and app deauthorized from etsy") + }catch{ + (error) + print(error) + ("failed to delete credentails and deauthorize app") + } + + }) + + }) + }) + - return true } - } + + + //MARK: - Request Methods extension EtsyRESTAPIManager { diff --git a/Vendoo/NetworksTableViewController.swift b/Vendoo/NetworksTableViewController.swift index c1d830c..f4a92d9 100644 --- a/Vendoo/NetworksTableViewController.swift +++ b/Vendoo/NetworksTableViewController.swift @@ -27,6 +27,7 @@ class NetworksTableViewController: UIViewController { private var networksDictionary: Dictionary = ["ebay":false, "amazon":false,"etsy":false,"facebook":false] private var itemListingDictionary: Dictionary! = Dictionary() private var graphManager: FacebookGraphAPIManager! = nil + private var etsyManager: EtsyRESTAPIManager! = nil @@ -149,13 +150,19 @@ extension NetworksTableViewController: UITableViewDataSource { case 2: //loads network cell for etsy cell = (self.tableView.dequeueReusableCellWithIdentifier("etsy", forIndexPath: indexPath) as! EtsyTableViewCell) + + let tabBar = self.tabBarController + if(((tabBar as? HomeViewController)?.etsyManager.isAuthorized)!){ + + (cell as! EtsyTableViewCell).networkToggle.setOn(true, animated: false) + } break default: //loads network cell for facebook cell = (self.tableView.dequeueReusableCellWithIdentifier("facebook", forIndexPath: indexPath) as! FBTableViewCell) let tabBar = self.tabBarController - if((tabBar as? HomeViewController)?.fbGraphManager.isAuthorized == true){ + if(((tabBar as? HomeViewController)?.fbGraphManager.isAuthorized)!){ (cell as! FBTableViewCell).networkToggle.setOn(true, animated: false) } @@ -396,7 +403,7 @@ extension NetworksTableViewController: UITableViewDelegate { //OAuthorization code for etsy - if(((self.tabBarController as? HomeViewController)?.fbGraphManager.isAuthorized)!){ + if(((self.tabBarController as? HomeViewController)?.etsyManager.isAuthorized)!){ dispatch_async(dispatch_get_main_queue(), { () -> Void in cell.networkToggle.setOn(false, animated: true) @@ -405,8 +412,8 @@ extension NetworksTableViewController: UITableViewDelegate { //code to deauthorize network let tabBar = self.tabBarController - let didSucceed:Bool = ((tabBar as? HomeViewController)?.etsyManager.deAuthorizeApp(self))! - cell.networkToggle.on = !didSucceed + ((tabBar as? HomeViewController)?.etsyManager.deAuthorizeApp(self))! + cell.networkToggle.on = ((tabBar as? HomeViewController)?.etsyManager.isAuthorized)! } else{ @@ -418,8 +425,10 @@ extension NetworksTableViewController: UITableViewDelegate { //this is the type of code desired to access the rest management classes - let tabBar = self.tabBarController - (tabBar as? HomeViewController)?.etsyManager.authorizeApp(self) + let tabBar = self.tabBarController + ((tabBar as? HomeViewController)?.etsyManager.authorizeApp(self))! + + cell.networkToggle.on = ((tabBar as? HomeViewController)?.etsyManager.isAuthorized)! //self.etsyManager.authorizeApp(self)