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)")
})
}
})
}
}
})
Related
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/
Hi to my fellow developers
Just a simple question. How can I get the data of user through Facebook Graph API after login in app?
There's a sample in Facebook Documentation, but its not working.
let permisions = ["public_profile" ,"email" ,"user_birthday"]
let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
fbLoginManager.logInWithReadPermissions(permisions, fromViewController: vc) { (result, error) -> Void in
if (error == nil) {
if result.isCancelled {
//do error handling for canceled
} else {
let fbLoginResult : FBSDKLoginManagerLoginResult = result
let facebookToken = fbLoginResult.token.tokenString
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).startWithCompletionHandler({ (connection, result, error) -> Void in
if (error != nil){
// handle error
}
let email = result.valueForKey("email") as? String ?? ""
let firstName = result.valueForKey("first_name") as? String ?? ""
let lastName = result.valueForKey("last_name") as? String ?? ""
// do stuffs with email or first name or last name
})
this is swift version 2.3 but with little modification it will convert to swift 3
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/
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!
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)"]