我一直在尝试使用内置的 iOS 帐户和 Twitter 框架来检索 Twitter 用户数据。
我正在成功访问手机上的用户帐户。但是,在向 twitter 发出 SLRequest 时,我收到一个未经身份验证的错误。我的印象是,将用户 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.";
}
);
}