Hi, welcome back on Facebook, that's the topic of this video.
What will specifically do in this video is willing to access dots of Facebook profiles via Python.
And we're going to do that using the official Facebook API.
So you are going to learn how to use the Facebook API from within Python so that you can batch access
data from profiles such as getting their photos and videos and posts, access and comments.
But this is not about hacking, so we're not going to access data for which we don't have permission.
So, for example, if we get photos from a Facebook account, we can only get those photos if those
are public or our accounts and that account are connected as friends and those photos are visible to
friends only.
So again, this is the official Facebook API that means everything is done according to the rules.
The benefit is that with Python, then you can create your own apps by accessing Facebook data, and
then you can use those dots as your wish in your Python program.
So get ready and let's do this.
The requirements for this is naturally a Facebook account, so you should have a Facebook account.
And so you should be logged into that account in your browser and then Google Facebook developer and
then click the first results, which should take it to developers dot Facebook dot com.
What we'll do here is we will create a developer account for Facebook, so that allows us to access
Facebook data programmatically.
So using the Facebook API.
So once you go to that page, go to get started.
And then you want to follow this process, what do you want to register as a developer?
You do need to enter a phone number in here in this step, I need to answer my phone number, so please
do that as well.
It's a requirement to have a Facebook developer account, so I answer my phone number.
I receive a code through an SMS.
I enter that code to the form in here.
So I verified my phone number and then I also confirm my email address as the form asks me to do.
Now we are on this step here.
So which of the following describes you, well developed were so prescient, pleads for registration,
then that should take us to this page.
So now we need to create an app.
So this is how it works with Facebook any time you want to do something.
It means you want to build an app for Facebook.
So and that's what we're doing.
So we want to access some photos or posts or comments from a Facebook profile, which we have access
to.
So create an app.
I'll choose none here.
This is mostly for Facebook statistics, they want to know what you're using these things for.
So for display name, put something or that's up, for example.
And then that's the email address of.
Press on, create up.
Re enter your Facebook password, so this is a password of your Facebook account.
And that should take us to these dashboards of our app.
So you see all this app is selected in here.
Those are all the settings.
Of my app.
The next thing we want to do now is to create an access token.
So go over to this header, so click there to go to the very home page in here and then go to more.
And then go to tools and then go to Graph API Explorer.
So Graph API is the name of the Facebook official API.
That should take you to this Web page.
Now make sure that in here on the Facebook app, your app is selected, so all its app is the name of
the app that I created a minute ago.
And then you want to generate an access token, so press and generate access token and then you want
to give access permission to your app.
So Facebook is making sure that you, as the owner of your Facebook account, are giving permission
to this or this app, the new app.
Now we do get this message here, which is a security feature or Facebook.
So because my Facebook account Ace New, I just created this account exclusively for this lecture,
I didn't want to use my main Facebook account.
So this new Facebook account, which was created just ten minutes ago, can not work with new applications.
So I need to wait 60 Minutes and then I can go back here and generate that access token.
So if you also have just created your Facebook account, then you also need to wait.
So maybe do some other videos in the course.
Otherwise just continue and follow this video.
60 Minutes have passed, so now I have access to the Graph API explorer interface.
So what is this and what can we do here?
Well, this is the interface of graphical interface that we can use to interact with a graph API explorer
of Facebook.
So using this interface is one way to interact with the API.
Another way is to use Python.
Both this interface gives us some key data that we need, and then we can switch to Python.
So let's explore this interface first.
What do we have here?
Well, we have the access token.
This one in here, you can press the button to copy it and put it in your Python script.
I'll do that later.
For now, it's important to know that you need to be careful with this access token where you put it,
especially if this is your main Facebook account.
For me, this is just a throwaway account, so.
This just by phone, is how I described this, as I told you, so I'm not going to worry much about
that, and I'm down here, you have the ad that we created the API app.
So are this app in my case?
No, this indicates if you're working with a user accounts or a page.
So we have Facebook pages and we have user calls here we are working on Facebook user accounts.
So this should be user token here and then we have permissions.
So currently we only have permission to the public profile of this user.
Accounts by phone is how, in my case, that to me is the API can only access posts and photos which
are public.
For example, this posting here is not public.
This is shared only with friends.
Therefore, with this permission, we cannot access that post.
Therefore, what we want to do here is search for user posts and presence and also you want to give
access to photos, so user photos as well.
And leave them there, once you have done that, then you need to press again on generates access token
so that you gets a new token for those permissions.
Right now, you see that these are not green anymore, so they have been added to permissions.
And this is a new token.
So that's the new token you need to use now if you want to access user photos and user posts.
That token is different from the previous form, so you can discard the previous token.
Don't use it.
Then we jump into this area here.
Let me zoom in.
We have this URL are a mixture moorfields equal I.D. common name.
And I'm going to press submit.
So what I did now is I performed a query to the API and we got back some data on what is this, what
we have the I.D. and we have the name.
The I.D. is the user I.D. of my Facebook account.
So all Python is told and that this is a name that vets use it to contest in.
In your case, it could be your Facebook name and surname or whatever you have in your Facebook profile
name.
So we just quoted some data with this query, but we did that through this interface.
So how can we do the same via Python?
Well, to do the same via Python, you need to go down here to get code presents and then you have different
types here, so you have JavaScript implementation of this.
But what we need to do is go to see your URL and then copy that entire thing.
So right click copy or control, see and then go to Python and I'm going to import requests first.
That's the Python third party library that allows you to make a request to the Graph API explorer.
So in the case of the VA interface, this interface makes a request to the API.
In this case, we want to make requests through this request library from Python and then construct
the URL you want to request or remove that part and the leaf only of double quotes followed by HTP as
graph, etc. And it closes with a double quotes.
So that's a Python string.
No.
And then we do respond to requests that get and we answer that you went in there and then we print out
a response, the content and run.
Request is going to be installed on my repo because it was not installed, and we should be seeing the
results in just a bit.
Yes, so that's the results is the same data that we got in here.
Now just one thing you see this Barbie here, which means this is a big string.
So if you want to get a plane stream, you want to say Texea instead of content, we're on again and
then you should get a plane string, not be in front.
So what's going on is well, and here you have this access token and this access token is fetched automatically
by this interface in Python, though when you copy that's €c codes.
What we copy is we also copy the access token.
So you see this part of your access token is equal to the actual access token, which you find also
in here.
So that means access token now is included in here.
That's my access token.
And then we have this other ports onto here.
So that's the base URL of the Graph API graph.
That's Facebook dot com v thirteen point zero.
And then we have this Port Me question mark.
Fields is equal to D, and we have this percentage too, which means a empty space, a white space.
And then we have a name.
So that is the same as we see in here, more or less the same, although the commas are white spaces
in here.
So let me explain what this means.
Well, MI is the equivalent of placing this idea.
So this is your unique I.D. of of your Facebook user account.
So even if I place that idea instead of me in here and I run, I will get the same results.
So that's the standards, you know, and you can do the same in here, so you can place that idea instead
of me.
And then followed by question mark submits and you get the same results.
So often have a question mark when we have the query, what fields do we need from this user account?
Well, we need the I.D. and we need the name.
And if we add posts, as I just did, it gives us also the information about posts.
So let's see what we've got here.
So we have Heidi, as the key of this dictionary.
We have name as another key.
So each key has its own value.
Python Nesto is the value of name, and then we have the post's key supports has as a value of this
dictionary.
And so this is a dictionary which has another key Delta and Delta has as a value of this list of this
list.
The dictionaries.
So this is one dictionary.
It starts here, it ends in here.
And this dictionary contains one posts, so it represents one Facebook post on my user account.
Here I have two posts.
This is this is my first post on Facebook and it says this comment.
And that's another post.
So two posts and here as well, we get these two posts.
One here, you see, this is my second post.
And then we have this other one in here.
This is my first post.
And then each post has a 90.
Let me expand this a bit.
So that's the idea of the post.
Now you ask, what's all these other things?
So we have two posts, then we have this other dictionary here.
So one, two, three and four.
What these are is photos.
So I have four photos here on my Facebook profile.
It's one to this is a Facebook main picture.
So it's one two three four, actually without the tiger.
And that's what we have in here now, let's repeat the same query in Python.
So again, get.
Go to €c and copy that entirely.
Go to Python in the URL paste.
That's clean it out.
We run and we should get the same dictionary in here.
So that's the dictionary is the same one with this in here.
Next, how can we get one single posts?
We have multiple polls.
How can we get just one?
Or how can we get to one single photo?
Let me show you that.
Now what you do is you need the I.D. of the posts that you want.
So for example, this second post here we give the I.D. with place.
It's in the beginning in here.
So that is followed by a question mark.
And then what fields do we need if we run this query as it is?
We're going to get this error tried accessing not existing fields posts.
Of course, it's not a field of one post, so ID name and posts were fields of the user accounts.
So it gives us the idea of the user calling the name of the user account and the posts.
So these are attributes of the user account, but these are not attributes or a pause.
So for posts, you could have comments.
Now this post the second post.
Doesn't have any comments, so I'm going to add a comma now.
Hey, this is a comment and then around this query again submits and then we have this common ski.
And so this is the text of a comment.
And the idea of a comment as well.
So everything has an idea.
And of course, you can do the same with this gets cold and put this in python to access it with requests.
Photos No.
Well, let's get to again me.
So with me, you get the user I.D. And so if you add the slash and write posts and press, enter again,
this is going to give you the posts and also the photos.
So again, try to get the idea of one post in here.
And of course, in Python, you can also iterate over these dictionaries and gets all the posts and
then make another loop where you access each posts.
You grab that idea and access it and then get the pictures.
If you only want the photos, though, you can remove posts and write photos.
But you also need to add slash uploaded to actually get the list of photos.
So now we have the photos and we also have a description of the photos, if there is any.
So when you upload the photo as a user on your Facebook account, it gives you the option to enter a
description.
So that's the description of the photo.
And as the idea of the photo.
So let's get this photo of this particular photo.
You want a copy of that I.D. pasted directly in here and then add the question mark fields if you want
to give any fields.
Otherwise, just leave it like that.
Submit that to give you vets.
If you want to get the link of the photo again, you want to add the question mark fields a single to
link.
Press submits, and that should give you this link, if you could, that's going to take you to this
page.
You want to follow the coupe case.
Try again yet.
So that's the link of the photo.
No, as you can see, this URL points to the photo page, not to the image file itself.
So if you want the actual image, you want to do something else.
So let's go back in here and sometimes you get this error of error validating access token.
The session is invalid.
That's happens because these tokens are only used for six minutes.
They are valid for that six minutes.
Then you want to press that button again to generate another access token.
And then we want to run that square again, which was this.
So in this new page with this new access token, submit.
Oh, I forgot to enter the first number it's on.
I missed one digit.
So again, so that's the Facebook page of the image.
Now if you had another here, common images.
So you'll see that this interface can also suggest you fields images and presence of myths.
Then you get this.
So you have this link.
Press it's a that's that points you to the actual image file.
So there are two links, actually.
One, you have this big image and want a smaller image.
So with five and then you can choose to download whichever you want.
So next, let's try to download this first image and see if it's an actual image file in Python.
So first of all, we need to get the codes.
Cockpit.
Place, it's in the other variable clean that's.
So that's zero.
This should give us that same dictionary.
So now we want to access a let's go again in here.
Not this one, this one.
So I want to look at here because we have a cleaner view of this dictionary, so we want to access the
images key.
Let's do that.
So data is equal to Respawn's the text and if we print just data images.
We get this error because Delta is actually a strength at this point.
So it's a strength, even though it looks like a dictionary, that dictionary is wrapped inside a string.
So we want to convert that string into an actual Python Dictionary object for that.
So we need to import Jason than that.
So it would be equal to JSON that loads.
And then we enter Delta.
So Delta currently is a string so that Delta is a string, but it will be converted into a dictionary.
So if I print it all now.
Now we have a DICT, so let's bring out the delta.
It looks the same as you will see is still the same, but now we can access the keys of the dictionary
because Python now knows how to pass that object.
So that's who gave us this list, which is basically, yeah, this one here.
That's a list.
Now let's get the first item of that list, which is this first dictionary.
So the list has to dictionaries this one, and that's another one.
So by applying an index of zero.
We get that first dictionary rights.
And then out of the dictionary, so we get this one.
And then out of that, we get the source key, the value of the source key.
Soldiers.
So that gives us the URL, which if you click it, it takes you to that image.
No, next.
Let's save this URL to an image.
You are a variable.
Remove the parentheses of the print function.
And then what we want to get now is to get the contents of that image, the Boyds.
And we do that.
With requests to gets again, and this request gets now, gets zero of the image images were so previously
it got it got the usual or that a web page and now it's a you or a loved one image and we extract the
content.
So now we want the content, not texts.
So we were working with bytes.
Images are binary, files are not, they are not text files.
And then we open a file locally with open image the chief p g in right binary mode as file and in that's
new file object.
We were right.
Image voids that variable and we run the script adds an image.
The JPG chief file is downloaded.
We click it and we see the actual cat image.
So that's how we can get an actual photo from Facebook via Python.
And lastly, if you want to see the documentation, you want to go to docs and just over the mouse there
and then go down to Graph API a reference.
And even here in this listing here.
You'll see all the notes that you can get.
So, for example, we worked with photos.
So I'm going to search for photos.
It's a long document.
So photo is something that we got.
If you click that, it'll take you in here.
So if you scroll down, for example, you see a table of fields that you can pass to photos.
So in this example, here we pass the link field for photos, but you can pass all the fields as you
see in this table.
So with that, I wanted to finish this video.
There's more and more about the Facebook API, and of course, I cannot cover it.
All of the points of visibility was to give you some orientation on how to start to get started with
a Facebook API.
The things you can do with the Facebook API are infinite, so I hope this helps you to get some ideas.
And whenever you need the Facebook API, just click this video to get started.
Thank you.
You.