【发布时间】:2014-08-27 15:21:59
【问题描述】:
所以我正在尝试使用 SLRequest 获取用户 Facebook 个人资料图片。我觉得我已经搜索了整个互联网都无济于事,而且我束手无策。这就是两难境地...
第一版代码:
let store = ACAccountStore()
let type = store.accountTypeWithAccountTypeIdentifier(ACAccountTypeIdentifierFacebook)
store.requestAccessToAccountsWithType(type, options: [ ACFacebookAppIdKey: "1437725166510606", ACFacebookPermissionsKey: ["email"] ]) { (granted: Bool, error: NSError!) -> Void in
if granted {
let accounts = store.accountsWithAccountType(type)
if let account = accounts.last as? ACAccount {
let pictureURLString = "https://graph.facebook.com/v2.1/me/picture"
let request = SLRequest(forServiceType: SLServiceTypeFacebook, requestMethod: SLRequestMethod.GET, URL: NSURL(string: pictureURLString), parameters: nil)
request.account = account
request.performRequestWithHandler() { (data: NSData!, response: NSHTTPURLResponse!, error: NSError!) -> Void in
if let imageData = data {
// Save the image
// println("Data size: \(imageData.length)\ndata: \(imageData.description)\nAs string: \(NSString(data: imageData, encoding: NSUTF8StringEncoding))")
data.writeToFile(NSFileManager.defaultManager().profileImagePath(), atomically: true)
}
}
}
}
}
好的,所以这个版本可以工作,但会返回一个非常非常小的个人资料图片版本。我想要更大的图像!根据 Facebook 文档以及 SO 上的许多其他人,这样做的方法是指定参数,例如:type=large 或 width=120&height=120,但一旦我这样做,我就会收到以下错误:
{"error":{"message":"An active access token must be used to query information about the current user.","type":"OAuthException","code":2500}}
当用于获取个人资料图片的 Facebook 文档(https://developers.facebook.com/docs/graph-api/reference/v2.1/user/picture)明确声明:
因为个人资料图片始终在 Facebook 上公开,所以此调用确实 不需要任何访问令牌。
许多建议,例如这个答案https://stackoverflow.com/a/7882628/1175289,建议在请求中使用 Facebook id 而不是“我”,但现在我们得到的是 app_scoped_user_id 而不是规范的 fbId,这似乎根本不起作用。
编辑:这很好用,我只是一个木板! :)
为了清楚起见,这里是导致错误的代码:
let store = ACAccountStore()
let type = store.accountTypeWithAccountTypeIdentifier(ACAccountTypeIdentifierFacebook)
store.requestAccessToAccountsWithType(type, options: [ ACFacebookAppIdKey: "1437725166510606", ACFacebookPermissionsKey: ["email"] ]) { (granted: Bool, error: NSError!) -> Void in
if granted {
let accounts = store.accountsWithAccountType(type)
if let account = accounts.last as? ACAccount {
let pictureURLString = "https://graph.facebook.com/v2.1/me/picture?type=large"
let request = SLRequest(forServiceType: SLServiceTypeFacebook, requestMethod: SLRequestMethod.GET, URL: NSURL(string: pictureURLString), parameters: nil)
request.account = account
request.performRequestWithHandler() { (data: NSData!, response: NSHTTPURLResponse!, error: NSError!) -> Void in
if let imageData = data {
// Save the image
// println("Data size: \(imageData.length)\ndata: \(imageData.description)\nAs string: \(NSString(data: imageData, encoding: NSUTF8StringEncoding))")
data.writeToFile(NSFileManager.defaultManager().profileImagePath(), atomically: true)
}
}
}
}
}
如您所见,唯一改变的是在 url 字符串中添加了 ?type=large。
如果有人遇到过类似的问题,或者知道我做错了什么,我们将不胜感激! :)
【问题讨论】:
标签: facebook facebook-graph-api ios7 slrequest