I'm trying to get data on a facebook user by using his ID.
I have an array of facebook user ids called IDs, and I set the graphPath to: "/IDs[0]", when I run the code I get an error.
cam you tell me whats wrong with my code?
var pathIs: String = "/"
pathIs += IDs[0]
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: pathIs, parameters: ["fields": "id, name, birthday, picture.type(small)"])
graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
if ((error) != nil)
{
// Process error
println("Error: \(error)")
}
else
{
println("fetched user: \(result)")
}
})
Thanks!
Related
I am attempting to retrieve the image from a facebook page. I have retrieved the page ID, and now I need to use this ID to get the image from the page. I am using the following code:
let pictureRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: picPath, parameters: ["fields" : "data"], HTTPMethod: "GET")
pictureRequest.startWithCompletionHandler({ (connection: FBSDKGraphRequestConnection!, result, error) -> Void in
if (error != nil) {
print("picResult: \(result)")
} else {
print(error!)
}
})
I am not getting an error, but my result is null. The picpath looks like this:
/110475388978628/picture
I copied the code directly from here but it isn't working. Any Idea what I'm doing wrong? I have the access token because I am able to get the page ID through the graph request
If you want to get the picture in the same request as the rest of the users information you can do it all in one graph request.
let request = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, email, picture.type(large)"])
request.startWithCompletionHandler({ (connection, result, error) in
let info = result as! NSDictionary
if let imageURL = info.valueForKey("picture")?.valueForKey("data")?.valueForKey("url") as? String {
//Download image from imageURL
}
})
there is no parameter called "fields". replace ["fields" : "data"] with nil
let pictureRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: picPath, parameters: nil, HTTPMethod: "GET")
pictureRequest.startWithCompletionHandler({ (connection: FBSDKGraphRequestConnection!, result, error) -> Void in
if (error != nil) {
print("picResult: \(result)")
} else {
print(error!)
}
})
You can use the Graph API explorer tool to help construct the graph request first: https://developers.facebook.com/tools/explorer
In this case, you'll see that you want the graph path to be just the object id (i.e., your picPath should be just 110475388978628 and your parameters should be [ "fields" : "picture"].
Then you'll want to parse the "url" out of the result["picture"]["data"]["url"].
let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
fbLoginManager.logInWithReadPermissions(["email"], fromViewController: self) { (result, error) -> Void in
if (error == nil){
let fbloginresult : FBSDKLoginManagerLoginResult = result
if (fbloginresult.grantedPermissions != nil){
if(fbloginresult.grantedPermissions.contains("email"))
{
self.getFBUserData()
}
}
}
func getFBUserData(){
if((FBSDKAccessToken.currentAccessToken()) != nil){
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email, gender, user_birthday, phone"]).startWithCompletionHandler({ (connection, result, error) -> Void in
if (error == nil){
//everything works print the user data
print(result)
}
})
}
}
I am trying to get facebook user profile details.
i am not getting birthday date and phone when i try to access it
How to change my code to get user's birthday date.
any help will be appreciated.thanks in advance
Make this change in ur code
func getFBUserData(){
if((FBSDKAccessToken.currentAccessToken()) != nil){
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email, gender, birthday, phone"]).startWithCompletionHandler({ (connection, result, error) -> Void in
if (error == nil){
//everything works print the user data
print(result)
}
})
}
}
And in Permissions array add
#"user_birthday"
Facebook provide 3 permission email, user_friends and public_profile for that you dont need to put your app in facebook review. remain all you need to put your app for the review at developer.facebook.com
Here are all about permission:
https://developers.facebook.com/docs/facebook-login/permissions
Facebook clearly said:
let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
fbLoginManager.logInWithReadPermissions(["email"], fromViewController: self) { (result, error) -> Void in
if (error == nil){
let fbloginresult : FBSDKLoginManagerLoginResult = result
if (fbloginresult.grantedPermissions != nil){
if(fbloginresult.grantedPermissions.contains("email"))
{
self.getFBUserData()
}
}
#23/10/2002
I'm having problems trying to get the user's country.
It was easy to get email, name, birthday, gender, hometown, etc... but when I try to get the user location I get nothing.
The code I'm using is:
Permisions:
loginManager.logInWithReadPermissions(["public_profile", "user_hometown", "user_location"], fromViewController: self)
The request:
FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"location"]).startWithCompletionHandler({ (connection, result, error) -> Void in
if error == nil{
print(result)
let location : NSDictionary! = result.valueForKey("location") as! NSDictionary
print(location)
}else{
print(error)
}
})
The result of this request is: {id = 1083969718300950;}
I read about FBSDKGraphRequest with parameters: nil, but it doesn't work anymore.
Someone knows how to get the country nowdays?
I use the lastest Facebook SDK in swift. My problem is when I try to pass my Facebook name into a variable. There is my code :
func returnUserData()
{
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil)
graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
if ((error) != nil)
{
// Process error
println("Error: \(error)")
}
else
{
// This variable is declared under my class declaration
self.userFacebook = result.valueForKey("name") as! String
println(self.userFacebook) //it's works, my Facebook name appear
}
})
}
But, when I try to use my variable "userFacebook" outsite of this function (returnUserData), the result is "nil".
I don't understand why my variable is set only in the function and not in my class.
Thanks
You pass parameter request what data is get for example get name.
request parameter like this
["fields": "id, name, first_name, last_name, picture.type(large), email"]
Try this code :
#IBAction func btnFBLoginPressed(sender: AnyObject) {
var fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
fbLoginManager .logInWithReadPermissions(["email"], handler: { (result, error) -> Void in
if (error == nil){
var fbloginresult : FBSDKLoginManagerLoginResult = result
if(fbloginresult.grantedPermissions.contains("email"))
{
self.getFBUserData()
fbLoginManager.logOut()
}
}
})
}
func getFBUserData(){
if((FBSDKAccessToken.currentAccessToken()) != nil){
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).startWithCompletionHandler({ (connection, result, error) -> Void in
if (error == nil){
println(result)
println(result["name"])
}
})
}
}
I'm trying to get the user's email address with the followings:
fbLoginManager.logInWithReadPermissions(["email", "public_profile"], handler: { (loginResult, error) -> Void in
if error != nil {
//handle error
} else if loginResult.isCancelled {
//handle cancellation
} else {
//Logged in
self.loggedinUser.fbToken = FBSDKAccessToken.currentAccessToken().tokenString
var graphReq = FBSDKGraphRequest(graphPath: "me", parameters: nil).startWithCompletionHandler { (connection, result, error) -> Void in
if let user = result as? NSDictionary {
var email = result.objectForKey("email") as? String
var name = result.objectForKey("name") as? String
self.loggedinUser.email = email ?? nil
self.loggedinUser.fullName = name ?? nil
}
}
}
})
But in the loginResult object the user's email is not sent. Any idea how to solve this?
thanks
You must provide in the parameters what you want to get:
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "email"]).startWithCompletionHandler({ (connection, result, error) -> Void in
For example to get more than one field:
parameters: ["fields": "id, name, email, picture.type(large)"]