1

I want to get user's profile picture from Facebook in my app. I am aware of the http request that returns the profile picture:

http://graph.facebook.com/USER-ID/picture?type=small

But for that I need the user-id of my user. Is there a way to fetch that from somewhere without using the Facebook SDK? If not, can someone show me a simple way to get the user's id (or the profile picture)?

bobsacameno
  • 765
  • 3
  • 10
  • 25
  • But there must be a user identifier right? How would your code magically knows that you need the profile picture of XYZ user? Second thing , the url that you've mentioned is not using any SDK, its a simple URL! – Sahil Mittal Jun 30 '14 at 12:25
  • I know that the url is just a simple url (that's the reason I like it). I thought that maybe the iPhone can know the user-id if the Facebook application is installed on it. – bobsacameno Jun 30 '14 at 12:29
  • r u get in the user profile ID – Anbu.Karthik Jun 30 '14 at 12:29
  • The user ID is the same as their FB Username, including any numbers, which you would need to locate them on FB anyway! – Steve Barnes Jun 30 '14 at 12:30
  • I know I can ask the user to put in his user name, and the app will perform this request. I wanted to find out if there's a way the iPhone already knows the user name. – bobsacameno Jun 30 '14 at 12:32

6 Answers6

9

try this... it's working fine in my code .. and without facebook id you cant get ..

and one more thing you can also pass your facebook username there..

 //facebookID = your facebook user id or facebook username both of work well
NSURL *pictureURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=large&return_ssl_resources=1", facebookID]]; 
NSData *imageData = [NSData dataWithContentsOfURL:pictureURL];
UIImage *fbImage = [UIImage imageWithData:imageData];

Thanks ..

Jogendra.Com
  • 6,394
  • 2
  • 28
  • 35
1

For getting FacebookId of any user, you will have to integrate Facebook Sdk from where you need to open session allow user to login in to Facebook (If user is already logged in to Facebook app, then it will just take permission from user to get access of the permission). Once you does that, you will get user details of logged in user from where you can get his FacebookId.

For more details please check developers.facebook.com.

Ashutosh
  • 2,215
  • 14
  • 27
0

In order to get the profile picture you need to know the Facebook user ID of the user. This can be obtained logging into Facebook with Social.framework (if you don't want to use Facebook SDK).

You can use ACAccountStore to request access to user's Facebook account like this:

ACAccountStore *accountStore = [[ACAccountStore alloc] init];

ACAccountType *facebookTypeAccount = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];

[accountStore requestAccessToAccountsWithType:facebookTypeAccount
                                       options:@{ACFacebookAppIdKey: YOUR_APP_ID_KEY, ACFacebookPermissionsKey: @[@"email"]}
                                    completion:^(BOOL granted, NSError *error) {
                                        ...

Please refer to instructions already answered in this post.

For information regarding how to obtain a Facebook App ID key (YOUR_APP_ID_KEY), look at Step 3 in this article.

Community
  • 1
  • 1
Markus Rautopuro
  • 7,997
  • 6
  • 47
  • 60
0

Though it's an older question, the same you can do with iOS SDK 4.x like:

Swift:

    let pictureRequest = FBSDKGraphRequest(graphPath: "me/picture?type=large&redirect=false", parameters: nil)
    pictureRequest.startWithCompletionHandler({
        (connection, result, error: NSError!) -> Void in
        if error == nil {
            println("\(result)")
        } else {
            println("\(error)")
        }
    })

Objective-C:

FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
                                  initWithGraphPath:[NSString stringWithFormat:@"me/picture?type=large&redirect=false"]
                                  parameters:nil
                                  HTTPMethod:@"GET"];
    [request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
                                          id result,
                                          NSError *error) {
    if (!error){
       NSLog(@"result: %@",result);}
    else {
       NSLog(@"result: %@",[error description]);
     }}];
Chanchal Raj
  • 4,176
  • 4
  • 39
  • 46
0

The answer is:

https://graph.facebook.com/v2.4/me?fields=picture&access_token=[yourAccessToken]

if your token is abcdef then url will be:

https://graph.facebook.com/v2.4/me?fields=picture&access_token=acbdef

According to API explorer

you can use this link

https://developers.facebook.com/tools/explorer?method=GET&path=me%3Ffields%3Dpicture&version=v2.4

then if you want to get code for any platform make this:

go to the end of page and press "Get Code" button

enter image description here

then in appeared dialog choose your platform

enter image description here

and you will see the code

Nikolay Shubenkov
  • 3,133
  • 1
  • 29
  • 31
0

The following solution gets both essential user information and full size profile picture in one go... The code uses latest Swift SDK for Facebook(facebook-sdk-swift-0.2.0), integrated on Xcode 8.3.3

import FacebookCore import FacebookLogin

@IBAction func loginByFacebook(_ sender: Any) {

     let loginManager = LoginManager()
        loginManager.logIn([.publicProfile] , viewController: self) { loginResult in
        switch loginResult {
        case .failed(let error):
            print(error)
        case .cancelled:
            print("User cancelled login.")
        case .success(let grantedPermissions, let declinedPermissions, let accessToken):
            print("Logged in!")

            let authenticationToken = accessToken.authenticationToken
            UserDefaults.standard.set(authenticationToken, forKey: "accessToken")

            let connection = GraphRequestConnection()
            connection.add(GraphRequest(graphPath: "/me" , parameters : ["fields" : "id, name, picture.type(large)"])) { httpResponse, result in
                switch result {
                case .success(let response):
                    print("Graph Request Succeeded: \(response)")
                /* Graph Request Succeeded: GraphResponse(rawResponse: Optional({
                 id = 10210043101335033;
                 name = "Sachindra Pandey";
                 picture =     {
                 data =         {
                 "is_silhouette" = 0;
                 url = "https://scontent.xx.fbcdn.net/v/t1.0-1/p200x200/13731659_10206882473961324_7366884808873372263_n.jpg?oh=f22b7c0d1c1c24654d8917a1b44c24ad&oe=5A32B6AA";
                 };
                 };
                 }))
                */

                    print("response : \(response.dictionaryValue)")



                case .failed(let error):
                print("Graph Request Failed: \(error)")
                }
            }
            connection.start()
        }
    }
}