Twitter SLRequest身份验证错误

时间:2016-03-21 14:33:50

标签: ios swift slrequest accounts-framework

我一直在尝试利用内置的iOS帐户和Twitter框架来检索Twitter用户数据。

我正在成功访问手机上的用户帐户。但是,当将SLRequest发送到twitter时,我收到一个未经过身份验证的错误。我的印象是将用户ACCount分配给twitter SLRequest.account满足了必要的OAuth部分。

代码来自我设置的测试项目

非常感谢任何反馈!

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])

根据我的发现,我认为下面的内容涵盖了Twitter api版本1.1的OAuth要求

      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")
  }
})

  }
}

从词典中返回的是什么:

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

1 个答案:

答案 0 :(得分:0)

params字典应该是:

["screen_name": "username"]