使用Facebook API,Firebase和Swift获取100x100个人资料照片

时间:2017-06-27 19:57:44

标签: facebook-graph-api firebase swift3

我的项目使用以下代码获取了中型个人资料照片的网址字符串:

 let downloadMediumPicTask = session.dataTask(with:       mediumProfPictureURL) { (data, response, error)
        in
        // The download has finished.
        if let e2 = error {
            print("Error downloading profile picture: \(e2)")
        } else {
            if let res2 = response as? HTTPURLResponse {
                print("Downloaded medium profile picture with response code \(res2.statusCode)")
                if let imageData2 = data {
                    mediumProfilePictureUIImageFile = UIImage(data: imageData2)!
                    print("mediumProfilePictureUIImageFile has now been defined as: \(mediumProfilePictureUIImageFile).")
                } else {
                    print("Couldn't get image: Image is nil")
                }
            } else {
                print("Couldn't get response code for some reason")
            }
        }
    }
    downloadMediumPicTask.resume()

它崩溃了,给出了403响应代码。正在引用的URL是来自Facebook的过期签名URL。 Firebase无法调整以获取新的相应网址,而且我从Firebase获取了该网址。我无法弄清楚如何直接获取它,如下所示:

   func getUrlOfMediumProfilePic(){

    if (FBSDKAccessToken.current() != nil) {
        let graphPathPart2 = "me/picture"
        let paramsPart2 = ["type":"medium", "redirect":"false"]
        let completionHandlerPart2 = { (connection: FBSDKGraphRequestConnection?, result: Any?, error: Error?) in

            if let error = error {
                print("Medium picture graph call contained an error: \(error.localizedDescription)")
                return
            } else {
                guard connection != nil else {
                    print("getURLOfLargeProfilePic() function aborted bc connection failed.")
                    return
                }
                let results = result! as! NSDictionary
                let dataDict = results["data"] as! NSDictionary
                stringOfMediumProfilePicURLaka100x100 = dataDict["url"] as! String
                print("medium picture graph call results define stringOfMediumProfilePicURLaka100x100 as: \(stringOfMediumProfilePicURLaka100x100)")
            }
        }
        let graphRequest = FBSDKGraphRequest(graphPath: graphPathPart2, parameters: paramsPart2)!
        graphRequest.start(completionHandler: completionHandlerPart2)
    }else{
        print("User not logged in when getURLOfMediumProfilePic() function was run.")
        return
    }
}

此代码在代码8中产生错误。

1 个答案:

答案 0 :(得分:0)

你试过这个:

https://graph.facebook.com/{id}/picture?width=100&height=100

我不太了解,所以我无法提供语法帮助。我想你可以向网址发出http请求并获取图片。 希望这有帮助:)