【问题标题】:iOS7 Access user Facebook Profile PictureiOS7 访问用户 Facebook 头像
【发布时间】: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


    【解决方案1】:

    因为您在 API 调用中使用 /me/,所以需要 access_token,因为 API 不知道 me 是谁。如果您将其替换为用户 ID,例如

    https://graph.facebook.com/v2.1/4/picture?type=large
    

    它应该可以正常工作。

    如果您想继续在 URL 中使用 /me/,只需将用户的 access_token 也附加到 URL 中,例如:

    https://graph.facebook.com/v2.1/4/picture?type=large&access_token=abcdef
    

    【讨论】:

    • 您好,谢谢您的回复。我现在想为我的愚蠢道歉!我曾认为在使用 app_scoped_user_id 时使用用户 ID 不起作用,而实际上我之前只是错误地构建了链接!正如你所说,它似乎确实有效!
    • 您不会碰巧知道为什么对graph.facebook.com/v2.1/10204218234286542/picture?type=large 的请求在浏览器中可以正常工作,但是当通过我的应用程序中的 SLRequest 请求时,返回:{"error":{"message" :"(#100) type 必须是以下值之一:small, normal, large, square","type":"OAuthException","code":100}} ??
    • @george-green 您必须将 type=large 参数添加为 SLRequest 参数字典的一部分,例如parameters:@{@"type":@"normal"}。然后它会工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多