ios - Twitter SLRequest 身份验证错误

标签 ios swift slrequest accounts-framework

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

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

代码来 self 设置的测试项目

非常感谢任何反馈!

var accountArray = 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.";
           }
        );
}

最佳答案

参数字典应该是:

["screen_name": "username"]

关于ios - Twitter SLRequest 身份验证错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36133846/

相关文章:

ios - 扩展 EXT_shader_texture_lod 不适用于 OpenGL ES 3.0 iOS

ios - 关闭 View Controller 在帮助器类 Swift 中不起作用

swift - 努力理解代码 - 它试图返回数字右侧的十进制数字 'n'

ios - 如何防止在下一段中突出显示表格行?

ios - 使用 SLRequest 在推文中发布图片 - 图片未显示

ios - 在didReceiveLocalNotification中收到本地通知后执行后台任务:

ios - 如何从 Xib 加载的 ViewController 设置 NavigationController navigationItem 标题

ios - Alamofire:发送 JSON 作为请求参数

facebook - requestAccessToAccountsWithType 后更新 UI

ios - 使用 Swift 从 iOS 应用程序发布到 Facebook