Get larger Facebook image through Firebase login (swift) - swift

I'm doing a login with Facebook through the Firebase login.
I do:
FBSDKLoginManager().logIn(withReadPermissions: [...], from: self) { (result, error) in
guard let current = FBSDKAccessToken.current() else { return }
guard let token = current.tokenString else { return }
let credential = FIRFacebookAuthProvider.credential(withAccessToken: token)
FIRAuth.auth()?.signIn(with: credential) { (user, error) in
//user.photoURL
}
}
user.photoURL contains the url of the image, but it is very small (100x100), I would like to retrieve the bigger image.
On this stackoverflow post they say to add ?type=large to the url but it doesn't work.
Any solution?

If you want to go through Facebook Graph then you can get the larger image from url
Here is an example of that, in this url you just need to pass your id, you will get larger image
http://graph.facebook.com/yourId/picture?width=400&height=400
For more info you can check this link

Related

Trouble Uploading Multiple Images to Firebase Storage

I have an array of image data(in bytes), and I want to upload them to storage and get the download URL for all of them. But after uploading lots of pictures, there is only one file appears in the storage under the reference. I do get the download URLs, but only the last one works, the rest all say access denied.
I suspect it is because I am putting all the pictures under the same storage reference and the picture substitutes every time. But what can I do to upload multiple images?
My upload code:
var picturesURL: [String] = [] // I want to put the download URL in this array
if self.imageButton.isSelected{
picturesURL = []
for photoData in self.pictures{ // pictures is the array that stores all the photo data
self.storage.child("images/file.png").putData(photoData, metadata: nil) { _, error in
guard error == nil else{
print("Failed to upload")
return
}
self.storage.child("images/file.png").downloadURL (completion: { url, error in
guard let url = url, error == nil else{
print("problems getting url")
return
}
let urlString = url.absoluteString
picturesURL.append(urlString)
print(urlString)
//UserDefaults.standard.set(urlString, forKey: "url")
})
}
}

Attempting to save username from twitter user to Firebase database iOS app

I'm attempting to save a twitter users username into the database for later reference my code below is executing but doesn't seem to be accessing the database or saving the username into the database and I'm really lost as to why. I'm attempting to have the username and userID so I can retrieve information about the user for a profile page in the app. So if I can avoid saving this data to the database all together that works too but I don't think it can be done that way.
fileprivate func setupTwitterButton() {
let twitterButton = TWTRLogInButton { (session, error) in
if let err = error {
print("Failed to login via Twitter: ", err)
return
}
// debug statement
//print("Successfully logged in using Twitter")
HUD.show(.labeledProgress(title: nil, subtitle: "Signing In"))
//we've authenticated twitter, time to log into firebase
guard let token = session?.authToken else { return }
guard let secret = session?.authTokenSecret else { return }
let creds = FIRTwitterAuthProvider.credential(withToken: token, secret: secret)
let dbref = FIRDatabase.database().reference()
let usersref = dbref.child("users")
let uid = session?.userID
//let user = FIRAuth.auth?.signIn
print("Creating user")
let newUserReference = usersref.child(uid!)
newUserReference.setValue(["username": session?.userName])
Okay so after some debugging it was pretty simple where I went wrong. I was trying to write to the database before I'd authenticated with the database. Once I had put my code for writing to the database after I'd authenticated it all worked correctly.

LinkedIn Share Data URL without Callback Response for Success or Error

I know there are so many answer related to share things over LinkedIn, but I do not get any exact scenario that how to share the data over LinkedIn. I use https://api.linkedin.com/v1/people/~/shares API for share, but there is no callback response for successes even error also. And the main thing is there is no dialog open for share.
Here is my code:
var Shareurl : String = "https://api.linkedin.com/v1/people/~/shares"
var payload : String = "{\"comment\":\"Check out developer.linkedin.com!\",\"content\":{\"Heloo Folks\":\"LinkedIn Developers Resources\",\"description\":\"Leverage LinkedIn's APIs to maximize engagement\",\"submitted-url\":\"https://developer.linkedin.com\",\"submitted-image-url\":\"https://brand.linkedin.com/etc/designs/linkedin/katy/global/clientlibs/img/default-share.png\"},\"visibility\":{ \"code\":\"anyone\" }}"
if LISDKSessionManager.hasValidSession()
{
// appInstance.showLoader()
LISDKAPIHelper.sharedInstance().postRequest(Shareurl, stringBody: payload, success: { (response) in
// appInstance.hideLoader()
let data = response?.data.data(using: String.Encoding.utf8)
do
{
let dictResponse : [String : AnyObject] = try JSONSerialization.jsonObject(with: data! as Data, options: JSONSerialization.ReadingOptions.mutableContainers) as! [String : AnyObject]
print("LINKED SHARE \(dictResponse)")
if pbSocialDelegate != nil
{
pbSocialDelegate?.getLinkedinResponse!(userData: dictResponse)
}
}
catch
{
print("error")
}
}, error: { (error) in
//appInstance.hideLoader()
print(error as Any)
})
}
Please help me that how to open share dialog of Linkedin, and the above code does not through any response or error.
LinkedIn does not provide any dialog, so if needed, then design your custom dialog.
If you're trying to share things over LinkedIn, do you really need the API for this? Check out the Microsoft LinkedIn Share URL Documentation. You should be able to share your URL or have others share some URL by setting the url param as indicated here:
https://www.linkedin.com/sharing/share-offsite/?url={url}

declined Permission handling Facebook Login

I am trying to force my users to provide their data in order to get access to my app.
Since the syntax in Swift 3 changed a little, I am stuck in developing this. Basically my idea is the following:
let permissions = ["public_profile", "user_birthday", "user_photos"]
PFFacebookUtils.logInInBackground(withReadPermissions: permissions, block:
{ (user, error) -> Void in
// casting user to FBSDKLoginManagerLoginResult
// asking for specific data declined/granted Permissions
if let user = user as? FBSDKLoginManagerLoginResult {
print(user.declinedPermissions)
print(user.grantedPermissions)
}}
My print method will never be called. What is the real way to cast from user(PFUser?) to FBSDKLoginManagerLoginResult ?
Try getting back the actual FBSDKLoginManagerLoginResult as the result instead of user.
let login:FBSDKLoginManager = FBSDKLoginManager()
let permissions = ["public_profile", "user_birthday", "user_photos"]
login.logIn(withReadPermissions: permissions, from: self) { (result: FBSDKLoginManagerLoginResult?, error: Error?) -> Void in
if(FBSDKAccessToken.current() != nil){
let permissions = FBSDKAccessToken.current().permissions
print(permissions)
//Do whatever else you need to do with the result
}

iOS7 Access user Facebook Profile Picture

So i'm trying to get a users Facebook profile image using SLRequest. I feel like I've scoured the entire internet to no avail and am at my wits end. Here's the dilemma...
Version 1 of the code:
let store = ACAccountStore()
let type = store.accountTypeWithAccountTypeIdentifier(ACAccountTypeIdentifierFacebook)
store.requestAccessToAccountsWithType(type, options: [ ACFacebookAppIdKey: "1437725166510606", ACFacebookPermissionsKey: ["email"] ]) { (granted: Bool, error: NSError!) -> Void in
if granted {
let accounts = store.accountsWithAccountType(type)
if let account = accounts.last as? ACAccount {
let pictureURLString = "https://graph.facebook.com/v2.1/me/picture"
let request = SLRequest(forServiceType: SLServiceTypeFacebook, requestMethod: SLRequestMethod.GET, URL: NSURL(string: pictureURLString), parameters: nil)
request.account = account
request.performRequestWithHandler() { (data: NSData!, response: NSHTTPURLResponse!, error: NSError!) -> Void in
if let imageData = data {
// Save the image
// println("Data size: \(imageData.length)\ndata: \(imageData.description)\nAs string: \(NSString(data: imageData, encoding: NSUTF8StringEncoding))")
data.writeToFile(NSFileManager.defaultManager().profileImagePath(), atomically: true)
}
}
}
}
}
Ok, so this versions works, but returns a really, really small version of the profile image. I want a larger image! According to the Facebook docs, and lot's of others on SO the way to do this is to specify parameters such as: type=large or width=120&height=120 but as soon as I do this I get the following error:
{"error":{"message":"An active access token must be used to query information about the current user.","type":"OAuthException","code":2500}}
When the Facebook docs for getting the profile image (at https://developers.facebook.com/docs/graph-api/reference/v2.1/user/picture) explicitly state:
Because profile pictures are always public on Facebook, this call does
not require any access token.
Many suggestions, such as this answer https://stackoverflow.com/a/7882628/1175289, suggest using the Facebook id rather than "me" in the request, but this does not seem to work at all now that we get an app_scoped_user_id rather than the canonical fbId.
EDIT: This works fine, I was just being a plank! :)
For the sake of sanity, here is the code that causes the error:
let store = ACAccountStore()
let type = store.accountTypeWithAccountTypeIdentifier(ACAccountTypeIdentifierFacebook)
store.requestAccessToAccountsWithType(type, options: [ ACFacebookAppIdKey: "1437725166510606", ACFacebookPermissionsKey: ["email"] ]) { (granted: Bool, error: NSError!) -> Void in
if granted {
let accounts = store.accountsWithAccountType(type)
if let account = accounts.last as? ACAccount {
let pictureURLString = "https://graph.facebook.com/v2.1/me/picture?type=large"
let request = SLRequest(forServiceType: SLServiceTypeFacebook, requestMethod: SLRequestMethod.GET, URL: NSURL(string: pictureURLString), parameters: nil)
request.account = account
request.performRequestWithHandler() { (data: NSData!, response: NSHTTPURLResponse!, error: NSError!) -> Void in
if let imageData = data {
// Save the image
// println("Data size: \(imageData.length)\ndata: \(imageData.description)\nAs string: \(NSString(data: imageData, encoding: NSUTF8StringEncoding))")
data.writeToFile(NSFileManager.defaultManager().profileImagePath(), atomically: true)
}
}
}
}
}
as you can see, the only thing that has changed is the addition of ?type=large to the url string.
If anyone has faced a similar issue, or has any idea what I'm doing wrong, help would be very much appreciated! :)
Because you are using /me/ in your API call, an access_token is required because the API doesn't know who me is. If you replace this with a User ID, e.g.
https://graph.facebook.com/v2.1/4/picture?type=large
It should work fine.
If you want to continue using /me/ in the URL, just append the user's access_token to the URL too, e.g.:
https://graph.facebook.com/v2.1/4/picture?type=large&access_token=abcdef