我正在使用 Swifter 库在我的 Swift iOS 8 应用程序中访问 Twitter:https://github.com/mattdonnelly/Swifter .问题是我从 Twitter 收到 401 未授权错误。我仔细检查了任何可能的原因:
- 消费者 key / secret 错误
- 确保不使用 API v1(使用 1.1)
在解决了这两个问题后(根据 Twitter 文档),我仍然面临这个问题。我认为这与我的身份验证方式有关。我正在尝试在不使用设备上的 ACAccount 的情况下访问公共(public)提要。
这是我的代码:
// 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)
}
更新:我一直在开发该应用程序,试图实现仅使用应用程序(而非基于用户)的身份验证和访问 token 来读取公共(public)时间线的功能。
我更新了代码以使用访问 token 和仅应用程序身份验证。不过还是不行。
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)
}
最佳答案
2015 年 2 月 3 日更新
您需要使用仅应用身份验证而不是传入 OAuth token 来向服务器进行身份验证。
除此之外,您在传递用户的屏幕名称时也没有正确请求带有 userId 的状态。您需要使用用户名获取用户标识,然后请求状态。
完整的工作代码如下:
required init(coder aDecoder: NSCoder) {
self.swifter = Swifter(consumerKey: "cKEY", consumerSecret: "cSECRET", appOnly: true)
super.init(coder: aDecoder)
self.swifter.authorizeAppOnlyWithSuccess({ (accessToken, response) -> Void in
self.twitterIsAuthenticated = true
}, failure: { (error) -> Void in
println("Error Authenticating: \(error.localizedDescription)")
})
}
@IBAction func getUserButtonPressed(sender: UIButton?) {
if (self.twitterIsAuthenticated) {
self.getTwitterUserWithName("erhsannounce")
} else {
// Authenticate twitter again.
}
}
func getTwitterUserWithName(userName: String) {
self.swifter.getUsersShowWithScreenName(userName, includeEntities: true, success: { (user) -> Void in
if let userDict = user {
if let userId = userDict["id_str"] {
self.getTwitterStatusWithUserId(userId.string!)
}
}
}, failure: failureHandler)
}
func getTwitterStatusWithUserId(idString: String) {
let failureHandler: ((error: NSError) -> Void) = {
error in
println("Error: \(error.localizedDescription)")
}
self.swifter.getStatusesUserTimelineWithUserID(idString, count: 20, sinceID: nil, maxID: nil, trimUser: true, contributorDetails: false, includeEntities: true, success: {
(statuses: [JSONValue]?) in
if statuses != nil {
self.tweets = statuses
}
}, failure: failureHandler)
}
看起来好像您没有通过服务器进行身份验证。
从您的代码中我可以看到您正在使用 OAuth 身份验证初始化但未能调用身份验证功能。
swifter.authorizeWithCallbackURL(callbackURL, success: {
(accessToken: SwifterCredential.OAuthAccessToken?, response: NSURLResponse) in
// Handle success
},
failure: {
(error: NSError) in
// Handle Failure
})
将其添加进去,然后调用您的 getTwitterTimeline()。
希望对你有帮助
关于swift - 使用 Swift 访问 Twitter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28734678/