Parsing Facebook SDK FBSDKGraphRequest result - ios

I want to check, if my current Facebook token (it´s present) still has all needed permissions granted by user.
So I call the graph to retrieve all granted permissions associated with my token:
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "permissions"]).startWithCompletionHandler({ (connection, result, error) -> Void in
var resultDictionary:NSDictionary!
if (error != nil) {
println("ERROR = \(error)")
}
else {
resultDictionary = result as! NSDictionary
println("1: ---- SUCCESS (result) ----")
println("\(result)")
// this doesn´t work:
// var test = resultDictionary.objectForKey("permissions")?.objectForKey("data")! ?? nil
// var count = test.count
// println("---- test (\(count))")
// println(test)
}
})
The result from Facebook is:
{
id = 646571222102633;
permissions = {
data = (
{
permission = "user_friends";
status = granted;
},
{
permission = "publish_actions";
status = granted;
},
{
permission = "public_profile";
status = granted;
}
);
};
}
Because there might be no/one/a lot of permissions, I want to iterate through the part of the result under permissions / data.
But I´m not familiar with the conversion of this NSDictionary into something where I can count the elements down under and iterate through.
Any help?

Got it!
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "permissions"]).startWithCompletionHandler({ (connection, result, error) -> Void in
var resultDictionary:NSDictionary!
if (error != nil) {
DebugOutput("ERROR = \(error)")
}
else {
resultDictionary = result as! [String: AnyObject]
println("---- SUCCESS (result) ----")
println("\(result)")
var test = resultDictionary.objectForKey("permissions")?.objectForKey("data")! as! [[String:AnyObject]]
var count = test.count
println("---- test (\(count))")
println(test)
}
})
Thanks a lot to: http://ustwo.github.io/tech-blog/2014/08/01/ios-swift-dictionaries/

Related

Use of undeclared type 'GraphRequestResult' after update of pods

I'm using the new facebook graph request and after updating pods I get an error
< Use of undeclared type 'GraphRequestResult'>
let graphRequest = GraphRequest(graphPath: kGraphPathMe, parameters: ["fields":"id,email,last_name,first_name,picture"], tokenString: accessToken.tokenString, version: .init(), httpMethod: .get)
graphRequest.start {(response: HTTPURLResponse?, result: GraphRequestResult<GraphRequest>) in
switch result {
case .success(let graphResponse):
if let dictionary = graphResponse.dictionaryValue {
completion(FacebookUser(jsonDict: dictionary))
}
break
default:
print("Facebook request user error")
}
}
check this code
let parameters = ["fields": "email, id, name"]
let graphRequest = FBSDKGraphRequest(graphPath: "me", parameters: parameters)
_ = graphRequest?.start { [weak self] connection, result, error in
// If something went wrong, we're logged out
if (error != nil) {
// Clear email, but ignore error for now
return
}
// Transform to dictionary first
if let result = result as? [String: Any] {
// Got the email; send it to Lucid's server
guard let email = result["email"] as? String else {
// No email? Fail the login
return
}
guard let username = result["name"] as? String else {
// No username? Fail the login
return
}
guard let userId = result["id"] as? String else {
// No userId? Fail the login
return
}
}
} // End of graph request

how to access all images from facebook in swift 5?

I am trying to access all the images form facebook that user uploaded
I try it with user_photos
https://developers.facebook.com/docs/facebook-login/permissions/#reference-user_photos
I also try graph API explore from this
iOS :How to get Facebook Album Photo's Picker
my code
for Login
func btnFbClicked()
{
fbLoginManager.logOut()
fbLoginManager.loginBehavior = .web
fbLoginManager.logIn(withReadPermissions: ["public_profile", "email", "user_friends","user_photos"], from: self) { (result, error) in
if error != nil
{
print("error occured with login \(String(describing: error?.localizedDescription))")
}
else if (result?.isCancelled)!
{
print("login canceled")
}
else
{
if FBSDKAccessToken.current() != nil
{
FBSDKGraphRequest.init(graphPath: "me", parameters: ["fields":"id, name, first_name, last_name, picture.type(normal), email,gender"]).start(completionHandler: { (connection, userResult, error) in
if error != nil {
print("error occured \(String(describing: error?.localizedDescription))")
}
else if userResult != nil {
print("Login with FB is success")
print(userResult as! [String:Any])
self.fbData = (userResult as? NSDictionary)!
let mutableFBDict = NSMutableDictionary(dictionary: self.fbData)
print(mutableFBDict)
if((mutableFBDict["email"] as? String) ?? "") == ""{
// self.showAlertNoHide(title: "", message: "If permission is granted then please set any email as a primary email in facebook general settings, then only facebook will return email to third party application. Sorry for the inconvenience.")
return
}
var gender = Int64()
if(mutableFBDict["gender"] as? String == "male")
{
gender = Int64(1)
}
else if(mutableFBDict["gender"] as? String == "female")
{
gender = Int64(2)
}
else
{
gender = Int64(0)
}
self.fetchListOfUserPhotos()
}
for fetching Photos
func fetchListOfUserPhotos()
{
guard let id = self.fbData["id"] as? String else
{
return
}
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: id + "/photos" , parameters: ["fields":"user_photos"], httpMethod: "GET")
//FBSDKGraphRequest(graphPath: id + "/photos" , parameters: nil, httpMethod: "GET")
graphRequest.start(completionHandler: { (connection, result, error) -> Void in
if ((error) != nil)
{
// Process error
print("Error: \(error)")
}
else
{
print("fetched user: \(result)")
let fbResult:[String:AnyObject] = result as! [String : AnyObject]
self.userPhotos = fbResult["data"] as! NSArray?
self.myCollectionView.reloadData()
}
})
}
Respons I get
Optional({
data = (
);
})
so my question is can access images that user uploaded on facebook or facebook change policies?
"fields":"user_photos"
user_photos is a permission, not a field for API requests. About the existing fields for photos, check out the API reference: https://developers.facebook.com/docs/graph-api/reference/photo/
If you don´t get any photos, debug your Access Token and make sure it really is a User Token that includes the user_photos permission: https://developers.facebook.com/tools/debug/accesstoken/

iOS facebookSDK get user full details

Iam using the last FBSDK (using swift)
// MARK: sign in with facebook
func signInWithFacebook()
{
if (FBSDKAccessToken.currentAccessToken() != nil)
{
// User is already logged in, do work such as go to next view controller.
println("already logged in ")
self.returnUserData()
return
}
var faceBookLoginManger = FBSDKLoginManager()
faceBookLoginManger.logInWithReadPermissions(["public_profile", "email", "user_friends"], handler: { (result, error)-> Void in
//result is FBSDKLoginManagerLoginResult
if (error != nil)
{
println("error is \(error)")
}
if (result.isCancelled)
{
//handle cancelations
}
if result.grantedPermissions.contains("email")
{
self.returnUserData()
}
})
}
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
{
println("the access token is \(FBSDKAccessToken.currentAccessToken().tokenString)")
var accessToken = FBSDKAccessToken.currentAccessToken().tokenString
var userID = result.valueForKey("id") as! NSString
var facebookProfileUrl = "http://graph.facebook.com/\(userID)/picture?type=large"
println("fetched user: \(result)")
}
when I print the fetched user I get only the id and the name ! ,
but i requested a permission for email and friends and profile ,
what's wrong ???
BTW : I moved this project from my macbook to another macbook ( because I formatted mine) it worked very well when it was at the the macbook which I created the project on , but after moving the project (using bitbucket clone) I got this results .
As per the new Facebook SDK, you must have to pass the parameters with the FBSDKGraphRequest
if((FBSDKAccessToken.currentAccessToken()) != nil){
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, email"]).startWithCompletionHandler({ (connection, result, error) -> Void in
if (error == nil){
println(result)
}
})
}
Documentations Link : https://developers.facebook.com/docs/facebook-login/permissions/v2.4
User object reference : https://developers.facebook.com/docs/graph-api/reference/user
With public profile you can get gender :
public_profile (Default)
Provides access to a subset of items that are part of a person's public profile. A person's public profile refers to the following properties on the user object by default:
id
name
first_name
last_name
age_range
link
gender
locale
timezone
updated_time
verified
Swift 4
An example in Swift 4 that also shows how to correctly parse out the individual fields from the result:
func fetchFacebookFields() {
//do login with permissions for email and public profile
FBSDKLoginManager().logIn(withReadPermissions: ["email","public_profile"], from: nil) {
(result, error) -> Void in
//if we have an error display it and abort
if let error = error {
log.error(error.localizedDescription)
return
}
//make sure we have a result, otherwise abort
guard let result = result else { return }
//if cancelled nothing todo
if result.isCancelled { return }
else {
//login successfull, now request the fields we like to have in this case first name and last name
FBSDKGraphRequest(graphPath: "me", parameters: ["fields" : "first_name, last_name"]).start() {
(connection, result, error) in
//if we have an error display it and abort
if let error = error {
log.error(error.localizedDescription)
return
}
//parse the fields out of the result
if
let fields = result as? [String:Any],
let firstName = fields["first_name"] as? String,
let lastName = fields["last_name"] as? String
{
log.debug("firstName -> \(firstName)")
log.debug("lastName -> \(lastName)")
}
}
}
}
}
I guess this code should help you get the required details
Swift 2.x
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil)
graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
if ((error) != nil)
{
// Process error
print("Error: \(error)")
}
else
{
print("fetched user: \(result)")
let userName : NSString = result.valueForKey("name") as! NSString
print("User Name is: \(userName)")
let userID : NSString = result.valueForKey("id") as! NSString
print("User Email is: \(userID)")
}
})
In Swift 4.2 and Xcode 10.1
#IBAction func onClickFBSign(_ sender: UIButton) {
if let accessToken = AccessToken.current {
// User is logged in, use 'accessToken' here.
print(accessToken.userId!)
print(accessToken.appId)
print(accessToken.authenticationToken)
print(accessToken.grantedPermissions!)
print(accessToken.expirationDate)
print(accessToken.declinedPermissions!)
let request = GraphRequest(graphPath: "me", parameters: ["fields":"id,email,name,first_name,last_name,picture.type(large)"], accessToken: AccessToken.current, httpMethod: .GET, apiVersion: FacebookCore.GraphAPIVersion.defaultVersion)
request.start { (response, result) in
switch result {
case .success(let value):
print(value.dictionaryValue!)
case .failed(let error):
print(error)
}
}
let storyboard = self.storyboard?.instantiateViewController(withIdentifier: "SVC") as! SecondViewController
self.present(storyboard, animated: true, completion: nil)
} else {
let loginManager=LoginManager()
loginManager.logIn(readPermissions: [ReadPermission.publicProfile, .email, .userFriends, .userBirthday], 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 : \(grantedPermissions), \n \(declinedPermissions), \n \(accessToken.appId), \n \(accessToken.authenticationToken), \n \(accessToken.expirationDate), \n \(accessToken.userId!), \n \(accessToken.refreshDate), \n \(accessToken.grantedPermissions!)")
let request = GraphRequest(graphPath: "me", parameters: ["fields": "id, email, name, first_name, last_name, picture.type(large)"], accessToken: AccessToken.current, httpMethod: .GET, apiVersion: FacebookCore.GraphAPIVersion.defaultVersion)
request.start { (response, result) in
switch result {
case .success(let value):
print(value.dictionaryValue!)
case .failed(let error):
print(error)
}
}
let storyboard = self.storyboard?.instantiateViewController(withIdentifier: "SVC") as! SecondViewController
self.navigationController?.pushViewController(storyboard, animated: true)
}
}
}
}
https://developers.facebook.com/docs/graph-api/reference/user

Unable to use FBSDKGraphRequest with Parse login

I'm using Parse to login with facebook on my app.
This is my code :
//set permissions required
let permissionsArray = ["user_about_me"]
//log in
PFFacebookUtils.logInWithPermissions(permissionsArray, block: { (pUser, pError) -> Void in
//if user is correctly loggin
if pUser != nil && pError == nil{
//request facebook for more informations
var request = FBSDKGraphRequest(graphPath: "me", parameters: nil)
request.startWithCompletionHandler({ (connection, result, error) -> Void in
NSLog("\(error)")
})
}
})
I am correctly log in with parse, but when i'am asking for my informations, have always have this message :
com.facebook.sdk:FBSDKGraphRequestErrorParsedJSONResponseKey={
body = {
error = {
code = 2500;
message = "An active access token must be used to query information about the current user.";
type = OAuthException;
};
};
code = 400;
}}
I have used Facebook SDK for login, i make a Parse login with returned token and i can get my user's informations after that.
I have solved my issue with this code :
FBSDKLoginManager().logInWithReadPermissions(["user_about_me"], handler: { (result, error) -> Void in
if let resultLogin : FBSDKLoginManagerLoginResult = result as? FBSDKLoginManagerLoginResult{
//if no error
if error == nil
&& resultLogin.grantedPermissions.contains("user_about_me"){
var accessToken = FBSDKAccessToken.currentAccessToken().tokenString
var expiration = FBSDKAccessToken.currentAccessToken().expirationDate
var facebookID = FBSDKAccessToken.currentAccessToken().userID
PFFacebookUtils.logInWithFacebookId(facebookID, accessToken: accessToken, expirationDate: expiration, block: { (pUser, pError) -> Void in
//if user is correctly loggin
if pUser != nil && pError == nil{
//request facebook for more informations
var request = FBSDKGraphRequest(graphPath: "me", parameters: nil)
request.startWithCompletionHandler({ (connection, result, error) -> Void in
NSLog("\(error)")
})
}
})
}
}
})

how to get posts from facebook page using GraphAPI in swift

My app had a functionality which is : get posts from Facebook page and show it in the app.
i searched and i found that i should use Facebook GraphAPI :
https://developers.facebook.com/docs/graph-api/using-graph-api/v2.3#reading
i follow the previous link, but i get error :
"An access token is required to request this resource."
i have the access token for the page, but i really don't know how to assign it in my request.
any help please?
thanks a lot!
1. Using Facebook SDK.
LOGIN FACEBOOK BUTTON
#IBAction func click_Facebook(sender: AnyObject)
{
let loginView : FBSDKLoginManager = FBSDKLoginManager()
loginView.loginBehavior = FBSDKLoginBehavior.Browser
loginView.logInWithReadPermissions(["public_profile","user_friends","user_photos","user_location","user_education_history","user_birthday","user_posts"], handler: { (result : FBSDKLoginManagerLoginResult!, error : NSError!) -> Void in
if ((error) != nil)
{
// Process error
self.alertWithMessaage(error.localizedDescription)
}
else if result.isCancelled {
// Handle cancellations
}
else {
self.returnUserData()
}
})
}
After login calling the below function.
var Requset : FBSDKGraphRequest
println("\(FBSDKAccessToken.currentAccessToken())")
var acessToken = String(format:"%#", FBSDKAccessToken.currentAccessToken().tokenString) as String
println("\(acessToken)")
var parameters1 = ["access_token":FBSDKAccessToken.currentAccessToken().tokenString]
Requset = FBSDKGraphRequest(graphPath:"me/posts", parameters:parameters1, HTTPMethod:"GET")
Requset.startWithCompletionHandler({ (connection, result, error) -> Void in
MBProgressHUD.hideHUDForView(appDelegate.window, animated: true)
if ((error) != nil)
{
println("Error: \(error)")
}
else
{
println("fetched user: \(result)")
var dataDict: AnyObject = result!.objectForKey("data")!
}
})
}
2. Using Default Facebook App.
// Get Access TOKEN
var _accountStore: ACAccountStore = ACAccountStore()
var accountType : ACAccountType = _accountStore.accountTypeWithAccountTypeIdentifier(ACAccountTypeIdentifierFacebook)!
var accounts : NSArray = _accountStore.accountsWithAccountType(accountType)!
var facebookAccount1: ACAccount = accounts.lastObject! as ACAccount
println("== \(facebookAccount1.credential.oauthToken)")
// Pass AccessToken To Parameter
var acessToken = String(format:"%#", facebookAccount1.credential.oauthToken) as String
var parameters = ["access_token":acessToken] as NSDictionary
//SLRequset
var imageURL : NSURL = NSURL(string: "Your graph URL")!
println("=== TOKEN : \(LoginService().currentAccount?.credential.oauthToken)")
var Requset = SLRequest(forServiceType: SLServiceTypeFacebook, requestMethod: SLRequestMethod.GET, URL: imageURL, parameters: parameters)
Requset.account = account
Requset.performRequestWithHandler { (responseData : NSData!, urlResponse : NSHTTPURLResponse!, error : NSError!) -> Void in
if (error != nil)
{
self.alertWithMessaage(error.localizedDescription)
}
else{
var datastring = NSString(data: responseData, encoding: NSUTF8StringEncoding)
println("== \(datastring)")
var error:NSError? = nil
var UserDict = NSJSONSerialization.JSONObjectWithData(responseData, options:nil, error: &error) as? NSDictionary
}
}
Swift 4.2 Update Code
in your viewDidLoad()
create faceBook Button for Login and Logout.
let loginButton = FBLoginButton(permissions: [ .publicProfile, .email, .userPosts, .userPhotos])
loginButton.center = view.center
view.addSubview(loginButton)
Now Create Function to get FaceBook Posts
func getPosts() {
let accessToken = AccessToken.current?.tokenString
let params = ["access_token" : accessToken ?? ""]
let request = GraphRequest(graphPath: "/me/posts/", parameters: params, httpMethod: .get)
request.start(completionHandler: { (test, result, error) in
if(error == nil)
{
print(result!)
}
})
}
In result you will get Data of your facebook posts!

Resources