facebook / facebook-ios-sdk

Used to integrate the Facebook Platform with your iOS & tvOS apps.
https://developers.facebook.com/docs/ios
Other
7.73k stars 3.51k forks source link

Profile picture URL returns 404 #2311

Open Isuru-Nanayakkara opened 6 months ago

Isuru-Nanayakkara commented 6 months ago

Checklist before submitting a bug report

Xcode version

15.0.1

Facebook iOS SDK version

14.1.0

Dependency Manager

SPM

SDK Framework

Login

Goals

I want to fetch the profile picture of the logged in user and display it in the app

Expected results

User's profile picture to be fetched and loaded in the image view

Actual results

The image URL I get from the API returns a 404 when called

Steps to reproduce

All the relevant code is added in the code samples section.

  1. I have a SwiftUi project and I added Facebook iOS SDK v14.1.0 via SPM.
  2. I have enabled public_profile permission on Facebook's app dashboard.
  3. I call the login() method on the LoginManager() which is successful and I get an access token.
  4. Then I initiate a GraphRequest for id, email, first_name, last_name, picture.type(large) fields which is also successful and for picture, I receive a URL like below.

    https://platform-lookaside.fbsbx.com/platform/profilepic/?asid=2972635969540266&height=200&width=200&ext=1704523207&hash=AfrYhf8UmigcI_Q8cgPi_mIc2h98N6pxXjnBaqxnE-3Osg

  5. I parse this URL to parseProfilePicture() method. I create a URL object with the above and pass it to a URLSession task to fetch its data.
  6. The request returns a 404 HTTP response.

There is a thread on this same issue on community forums that's been open for a month with no answer hence I'm posting it here for attention.

Code samples & details

import FacebookLogin

class LoginViewModel: ObservableObject {
    @Published private(set) var profilePicture: UIImage?

    public func login() {
        loginManager.logIn(permissions: ["public_profile", "email"], from: nil) { [self] loginResult, loginError in
            if let loginError {
                error = loginError.localizedDescription
            } else if let token = loginResult?.token {
                fetchProfileData()
            } else {
                error = "Something went wrong with login"
            }
        }
    }

    private func fetchProfileData() {
        let request = GraphRequest(graphPath: "me", parameters: ["fields": "id,email,first_name,last_name,picture.type(large)"])
        request.start { [self] _, profileResult, profileError in
            if let profileError {
                error = profileError.localizedDescription
            } else if let result = profileResult as? [String: Any] {
                parseProfilePicture(from: result)
            } else {
                error = "Something went wrong with profile data fetching"
            }
        }
    }

    private func parseProfilePicture(from result: [String: Any]) {
        let pictureData = result["picture"] as! [String: Any]
        let picture = pictureData["data"] as! [String: Any]
        let pictureDataURLString = picture["url"] as! String
        let pictureDataURL = URL(string: pictureDataURLString)!

        URLSession.shared.dataTask(with: URLRequest(url: pictureDataURL)) { [self] data, response, imageDownloadError in
            if let imageDownloadError {
                error = imageDownloadError.localizedDescription
            } else if let imageData = data {
                print("🌐 Status code: \((response as! HTTPURLResponse).statusCode)")
                print(imageData)
                DispatchQueue.main.async { [self] in
                    profilePicture = UIImage(data: imageData)
                }
            } else {
                error = "Something went wrong with downloading profile picture data"
            }
        }.resume()
    }
}
OS-ricardomoreirasilva commented 4 months ago

I've also come across this issue. Also noticed this that probably points to the fact that this is an issue only occurring with unpublished apps?