swift - 正确使用 facebook app-scoped user id

标签 swift facebook

有人可以帮我解决这个问题吗?

我的应用程序在 Facebook 上注册并设置为“实时”,它有一个 appID、一个 secret 和一个 token 客户端。 在我的应用程序中,userA 在 Facebook 上执行登录,我得到了 IdA(它是应用程序范围的)。 在我的应用程序中,userB 在 Facebook 上执行登录,我得到的 IdB 仍然在应用程序范围内。

现在,每当我尝试获取用户个人资料图片时,我都会收到错误消息。 这是我为用户 A 尝试的,对用户 B 也是如此:

https://graph.facebook.com/IdA/picture?type=small&access_token=appID|secret “消息”:“不支持的获取请求。ID 为“IdA”的对象不存在,由于缺少权限而无法加载,或者不支持此操作。请阅读 https://developers.facebook.com/docs/graph-api 处的图形 API 文档”, “类型”:“GraphMethodException”, “代码”:100, “错误子代码”:33

https://graph.facebook.com/IdA/picture?access_token=appID "message": "无效的 OAuth 访问 token 。", “类型”:“OAuthException”, “代码”:190 https://graph.facebook.com/IdA/picture?access_token=Token客户 "message": "无效的 OAuth 访问 token 。", “类型”:“OAuthException”, “代码”:190

有人可以阐明这一点吗? 提前致谢

最佳答案

您需要授予权限,然后您需要调用图形 API (FBSDKGraphRequest) 来获取个人资料图像。 我准备了一个类,你可以通过这个类实现你的要求:

import UIKit
import FBSDKCoreKit
import FacebookCore
import FacebookLogin

class LoginWithFacebook {

    // MARK: - Properties
    static let sharedInstance = LoginWithFacebook()


    /**
     Takes user to the facebook login page so that user could login with his facebook credentials and brings back basic details of the user and photo album
     - parameter viewControl: controller on which the facebook page will be opened
     - parameter completion: on completion the method returns user info in the form of dictionary
     */
    public func fBLoginWithPhotoAlbum(viewControl: UIViewController, completion: @escaping ([String: Any]) -> Void) {

        var responseDictionary: [String: Any] = [String: Any]()
        let loginManager = LoginManager()

        loginManager.logIn(readPermissions: [.publicProfile, .email, .userFriends, .custom("user_birthday")], viewController: viewControl) { (loginResult) in
            print(loginResult)
            switch loginResult {
            case .failed(let error):
                print(error)

            case .success(grantedPermissions: _, declinedPermissions: _, token: let accessToken) :

                responseDictionary.updateValue(accessToken.authenticationToken, forKey: "accessToken")

                let param = ["fields": "first_name, last_name, picture.width(9999), email, friends, gender, age_range, birthday, photos{album,picture}"]
                FBSDKGraphRequest.init(graphPath: "me", parameters: param).start { (_, result, _) -> Void in

                    if let resultDictionary: NSDictionary = result as? NSDictionary {
                        responseDictionary.updateValue(resultDictionary, forKey: "userInfo")

                        FBSDKGraphRequest.init(graphPath: "me/albums", parameters: ["fields": "photos{picture}"], httpMethod: "GET").start(completionHandler: { (_, result, _) in

                            let data = (result as? NSDictionary)?.value(forKey: "data") as? NSArray
                            if (data?.count)! > 0 {
                                var profileAlbum: [Any] = data!.filter { NSPredicate(format: "(name == %@) ", "Profile Pictures").evaluate(with: $0) }
                                if profileAlbum.count > 0 {
                                    if let profilePicAlbum = profileAlbum[0] as? NSDictionary {
                                        let graphPath = profilePicAlbum.value(forKey: "id")

                                        //picture, "photos{link}"
                                        let param = ["fields": "photos{picture}"]
                                        FBSDKGraphRequest.init(graphPath: graphPath as? String, parameters: param, httpMethod: "GET").start(completionHandler: { (_, result, _) in
                                            if let profilePicArray = (result as? NSDictionary)?.value(forKeyPath: "photos.data") {
                                                responseDictionary.updateValue(profilePicArray, forKey: "profilePics")
                                                completion(responseDictionary)
                                            }
                                        })
                                    }
                                }
                            } else {
                                completion(responseDictionary)
                            }
                        })

                    } else {

                    }
                }

            case .cancelled :
                completion(["message": "Something Went Wrong"])
            }

        }
    }
}

关于swift - 正确使用 facebook app-scoped user id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50186155/

相关文章:

ios - 如何在swift 4中获取从特定字符到字符串末尾的子字符串?

ios - 将开关状态绑定(bind)到 Swift 中的函数

python - 在 PythonAnywhere 上使用 Flask-Stormpath 登录 Facebook 会引发 JSONDecodeError

css - 是否可以设计新的 Facebook 页面插件的样式?

objective-c - 在 Facebook 上张贴照片

ios - 访问 '<>' 符号内的值

ios - 如何用Parse解决Swift中 "no results matched the query"的错误

php - 存储 Facebook 访问 token 的标准方法是什么?数据库还是cookie?

facebook - URL 更改,保留 Facebook 点赞

Xcode Swift 文件是为 arm64 构建的,它不是被链接的架构 (armv7)