简体   繁体   中英

Twitter SLRequest Authentication Error

I have been trying to retrieve twitter user data utilizing the built in iOS Accounts and Twitter frameworks.

I am getting access to the user account on the phone successfully. However, when making the SLRequest to twitter I receive an non authenticated error. I was under the impression that assigning the user ACCount to the twitter SLRequest.account fulfilled the OAuth parts necessary.

Code is from test project I have set up

Any feedback is greatly appreciated!

var accountsArray = AnyObject var account = ACAccount()

override func viewDidLoad() {
super.viewDidLoad()

let accountStore: ACAccountStore = ACAccountStore()
let twitterAccountType: ACAccountType = accountStore.accountTypeWithAccountTypeIdentifier(ACAccountTypeIdentifierTwitter)
let url = NSURL(string: "https://api.twitter.com/1.1/users/show.json")
let params: NSDictionary = ["field": "user_id, screen_name"]

accountStore.requestAccessToAccountsWithType(twitterAccountType, options: nil, completion: { (success, error) -> Void in

  if success {
     let account = accountStore.accountsWithAccountType(twitterAccountType)
    if let userAccount = account.first as? ACAccount {
      self.accountsArray.append(userAccount)

      let twitterRequest = SLRequest(forServiceType: SLServiceTypeTwitter, requestMethod: SLRequestMethod.GET, URL: url, parameters: params as [NSObject : AnyObject])

From what i have found i thought the line below covered the OAuth requirement for twitter api version 1.1

      twitterRequest.account = userAccount
      twitterRequest.performRequestWithHandler({ (responseData : NSData?, urlResponse : NSHTTPURLResponse?, error : NSError?) -> Void in

          print("data : \(responseData)")
          if let response = responseData {
            var dict = NSDictionary()
            do {
              dict = try! NSJSONSerialization.JSONObjectWithData(response, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
            }
            print(dict)
        } else if error != nil {
          print("Error : \(error)")
        }
      })
    }
  } else if (error != nil) {
    print("nooope")
  }
})

  }
}

Whats returned from the dictionary:

{
 errors = (
           {
            code = 50;
            message = "User not found.";
           }
        );
}

params字典应为:

["screen_name": "username"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM