How to detect default avatar on facebook?

假装没事ソ 提交于 2020-01-11 06:33:23

问题


How to detect default avatar on the link like this: https://graph.facebook.com/'.$id.'/picture?type=large? Is it the only way to get avatars (male/female) from special prepared profiles and then compare by e.g. md5() ?

It's hard to believe this is the only way.


回答1:


You can use the redirect=false parameter:

https://graph.facebook.com/naitik/picture?redirect=false

Then facebook's responce is json and contains this data:

{
   "data": {
      "url": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-prn1/157337_5526183_369516251_q.jpg",
      "is_silhouette": false
   }
}

You can use the is_silhouette option to detect if the photo is default one.

You can read more at: https://developers.facebook.com/docs/reference/api/using-pictures/




回答2:


There isn't an API you can call to tell if they are using the default photo or not. Instead of downloading the whole image and checking the MD5, you could issue an HTTP HEAD request to that profile URL and look at the Location header and see if the URL is one of the known default profile images:

Male: https://fbcdn-profile-a.akamaihd.net/static-ak/rsrc.php/v2/yL/r/HsTZSDw4avx.gif

Female (Darth Vader):https://fbcdn-profile-a.akamaihd.net/static-ak/rsrc.php/v2/yp/r/yDnr5YfbJCH.gif

These URL's could change I suppose, and so could the default photos, but I haven't seen either case happen that I can remember.




回答3:


If you are already making a call to the Graph API to get user data like the avatar, just include picture in the fields param when you make your first call to Graph API, then the response will include the is_silhouette offset, if it's set to true the user has the default avatar.

Request:

https://graph.facebook.com/v2.7/me?access_token=[token]&fields=name,picture

Response:

{
    "id": "100103095474350",
    "name": "John Smith",
    "picture": {
        "data": {
            "is_silhouette": true,
            "url": "https://scontent.xx.fbcdn.net/v/...jpg"
        }
    }
}



回答4:


Using Facebook SDK for iOS (Swift 4):

class FacebookSignIn {

    enum Error: Swift.Error {
       case unableToInitializeGraphRequest
       case unexpectedGraphResponse
    }

    func profileImageURL(size: CGSize, completion: @escaping Result<URL?>.Completion) {
       guard let userID = FBSDKAccessToken.current()?.userID else {
          completion(.failure(Error.unableToInitializeGraphRequest))
          return
       }
       let params: [String: Any] = ["redirect": 0,
                                    "type": size.width == size.height ? "square" : "normal",
                                    "height": Int(size.height),
                                    "width": Int(size.width),
                                    "fields": "is_silhouette, url"]

       guard let request = FBSDKGraphRequest(graphPath: "/\(userID)/picture", parameters: params) else {
          completion(.failure(Error.unableToInitializeGraphRequest))
          return
       }

       _ = request.start { _, result, error in
          if let e = error {
             completion(.failure(e))
          } else if let result = result as? [String: Any], let data = result["data"] as? [String: Any] {
             if let isSilhouette = data["is_silhouette"] as? Bool, let urlString = data["url"] as? String {
                if isSilhouette {
                   completion(.success(nil))
                } else {
                   if let url = URL(string: urlString) {
                      completion(.success(url))
                   } else {
                      completion(.failure(Error.unexpectedGraphResponse))
                   }
                }
             } else {
                completion(.failure(Error.unexpectedGraphResponse))
             }
          } else {
             completion(.failure(Error.unexpectedGraphResponse))
          }
       }
    }
}


public enum Result<T> {

   case success(T)
   case failure(Swift.Error)

   public typealias Completion = (Result<T>) -> Void
}


来源:https://stackoverflow.com/questions/10756323/how-to-detect-default-avatar-on-facebook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!