【发布时间】:2015-04-28 08:54:26
【问题描述】:
我正在使用 Swifter 库在我的 Swift iOS 8 应用程序中访问 Twitter:https://github.com/mattdonnelly/Swifter。问题是我从 Twitter 收到 401 Not Authorized 错误。我仔细检查了任何可能的原因:
- 用户密钥/密码错误
- 确保不要使用 API v1(使用 1.1)
解决了这两个问题(根据 Twitter 文档),我仍然面临这个问题。我认为这与我的身份验证方式有关。我正在尝试在不使用设备上的 ACAccount 的情况下访问公共提要。
这是我的代码:
// MARK: Twitter
var swifter: Swifter
required init(coder aDecoder: NSCoder) {
self.swifter = Swifter(consumerKey: "KEY", consumerSecret: "SECRET")
super.init(coder: aDecoder)
}
func getTwitterTimeline() {
let failureHandler: ((NSError) -> Void) = {
error in
self.alertWithTitle("Error", message: error.localizedDescription)
}
self.swifter.getStatusesUserTimelineWithUserID("erhsannounce", count: 20, sinceID: nil, maxID: nil, trimUser: true, contributorDetails: false, includeEntities: true, success: {
(statuses: [JSONValue]?) in
if statuses != nil {
self.tweets = statuses!
}
}, failure: failureHandler)
}
func alertWithTitle(title: String, message: String) {
var alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
更新:我一直在开发应用程序,试图实现仅使用应用程序(不是基于用户)身份验证和访问令牌来读取公共时间线的功能。
我更新了代码以使用访问令牌和仅应用程序身份验证。不过还是不行。
required init(coder aDecoder: NSCoder) {
let accessToken = SwifterCredential.OAuthAccessToken(key: "KEY", secret: "SECRET")
let credential = SwifterCredential(accessToken: accessToken)
self.swifter = Swifter(consumerKey: "cKEY", consumerSecret: "cSECRET", appOnly: true)
swifter.client.credential = credential
super.init(coder: aDecoder)
}
【问题讨论】: