How to retrieve user country using facebook Graph API? - facebook-graph-api

I want to retrieve the logged in user country, and insert it into a div.
I succeed making it with the user Name, Gender and Age range, but somewhy I can't retrieve the country.
Here is my code:
function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me?fields=name,email,gender,age_range,picture,country', function (response) {
document.getElementById('status').innerHTML = response.name + ",";
document.getElementById('status1').innerHTML = response.gender + ",";
document.getElementById('status2').innerHTML = (response.age_range.min) + "-" + (response.age_range.min + 1) + " years old";
document.getElementById('status3').innerHTML = response.country;
}
Thanks!

To complete what Lix said:
You can do it with only one call to Graph API, by calling
/me?fields=name,email,gender,age_range,picture,location{location}
Indeed, the location object under the user object is a Page object, which has a Location object.
So, this will give you something like this:
"location": {
"location": {
"city": "Ramat Gan",
"country": "Israel",
"latitude": 32.0833,
"longitude": 34.8167,
"zip": "<<not-applicable>>"
}
}
This avoids you to make a double call to Graph API.

I believe the parameter you are looking for is location instead of country.
/me?fields=name,email,gender,age_range,picture,location
In order to query this data, you'll need your users to grant you the user_location permission.
This will give you value of the user submitted field - take note that this parameter might not always be populated since it depends on the user actually submitting this information - if they have not provided it - you will not be able to retrieve it.
The object will look something like this:
"location": {
"id": "112604772085346",
"name": "Ramat Gan"
},
Once you have the location object (which will most likely be a page), you can query that object to retrieve the country:
/112604772085346?fields=location
This will give you more information including the country.
{
"location": {
"city": "Ramat Gan",
"country": "Israel",
"latitude": 32.0833,
"longitude": 34.8167,
"zip": "<<not-applicable>>"
},
"id": "112604772085346"
}

Fields in Fb API are now concatenable, so, for a detailed info of User's location you need:
scope="public_profile,user_location"
fields="name,location{location{country, country_code, city, city_id, latitude, longitude, region, region_id, state, street, name}}"

v2.11 query:
/me?fields=hometown,location
permission:
user_hometown
user_location
result:
{
"hometown": {
"id": "XXXXREDACTED",
"name": "Manila, Philippines"
},
"location": {
"id": "XXXXREDACTED",
"name": "Manila, Philippines"
},
"id": "XXXXREDACTED"
}

Related

how calculation be done in JSON object elements

I have declared instalment_2 as JSONField in the Django model.
My concern here is I am getting below error:
string indices must be integers
I tried all the ways which were available on the internet. But no luck.
I am passing this JSON as POST request:
"instalment_2": {
"type": "instalment_2",
"mode": "cash",
"date": "1/09/2019",
"amount": "35000"
}
It should calculate paid and balance based on installment amounts. instalment_1 amount is already in the table.
Now I am trying to do below.
instalment_2 = {
"type": body['instalment_2']['type'],
"mode": body['instalment_2']['mode'],
"date": body['instalment_2']['date'],
"amount": body['instalment_2']['amount']
}
paid = int(obj.instalment_1['amount']) + int(obj.instalment_2['amount'])
balance = int(obj.total) - paid

How to read and save the JSON format result that we obtained in Postman?

{
"message": "post created",
"authData": {
"user": {
"id": 1,
"username": "usha",
"Password": "ushausha"
},
"iat": 1554617615
}
}
This is the postman result that I obtained.
The user is a mock user,i.e, a const user
I want only the user part, i.e, the id, username and password to be read and saved separately.
You have 2 options:
1) there is a small copy button on the right of the result section:
click on it and the result is copied to your clipboard.
2) At the right section of History, you can save responses for all requests you have made:

Facebook Graph Api publishing to feed returns: "(#100) Param place must be a valid place tag ID"

I am searching at Facebook Graph Api, using graph api explorer, for some place using the following endpoint:
/search?type=place&q=centauro&fields=id,name,link
I am getting this as response:
"data": [
{
"id": "492103517849553",
"name": "Centauro",
"link": "https://www.facebook.com/Centauro-492103484516223/"
},
{
"id": "313439499156253",
"name": "Centauro",
"link": "https://www.facebook.com/Centauro-313439462489590/"
},
{
"id": "175812113006221",
"name": "Centauro",
"link": "https://www.facebook.com/Centauro-175812079672891/"
},
{
"id": "1423220914594882",
"name": "Centauro",
"link": "https://www.facebook.com/pages/Centauro/1423220891261551"
},...
When I try to publish using the field "id" returned:
/me/feed
with fields:
message: Testing
place: 492103517849553
I get the following reponse:
{
"error": {
"message": "(#100) Param place must be a valid place tag ID",
"type": "OAuthException",
"code": 100,
"fbtrace_id": "DfEKOjZX8g+"
}
}
But if I use de final number of the link:
"link": "https://www.facebook.com/Centauro-492103484516223/"
492103484516223
And try again:
/me/feed
with fields:
message: Testing
place: 492103484516223
It works perfectly.
So, is there a way to get te correct place id for publishing? Or is it a bug?
I was also getting the “(#100) Param place must be a valid place tag ID” error, but got it to go away by providing a JSON string within the 'place' element.
So where the content of your request was this:
place: 492103484516223
Format the place information like this instead:
place: {"id": "492103484516223"}
Currently, this is how you can solve it.
import requests
IG_USER_ID = <YOUR INSTAGRAM USER ID>
USER_ACCESS = <YOUR USER ACCESS TOKEN WITH VALID PERMISSIONS>
CONTAINER1_ID = <ID OF FIRST CONTAINER>
CONTAINER2_ID = <ID OF SECOND CONTAINER>
URL = f"https://graph.facebook.com/v13.0/{IG_USER_ID}/media?caption=Fruit%20candies&media_type=CAROUSEL&children={CONTAINER1_ID}%2C{CONTAINER2_ID}&access_token={USER_ACCESS}"
r = requests.post(URL)

Facebook API process shared object [duplicate]

I just tried through the Graph API Explorer with this path /v2.4/10153513872748291 and I've got this result:
{
"error": {
"message": "(#12) singular links API is deprecated for versions v2.4 and higher",
"type": "OAuthException",
"code": 12
}
}
But https://developers.facebook.com/docs/reference/api/post/ doesn't say anything about deprecation.
I'm not sure if I miss something, or there's another way to get info about an individual post.
Edit: v2.3 works, but v2.4 is the latest one.
Looks like you now need to the combination of the id of the user or page that made the post (or whose wall it is on), an underscore, and then the post id.
For your example post, 10153513872748291, that is made by a page Drama-addict, that has the id 141108613290 – so 141108613290_10153513872748291 will work.
And so does 788239567865981_10153513872748291, because 788239567865981 is the id of the user making the post.
Firstput userId underscore add postId /Likes to check Like status in facebook
userId_post_Id/Likes to fetch Likes Records
userId_post_Id/Comments to fetch Comments Records
https://developers.facebook.com/tools/explorer/145634995501895/?method=GET&path=303261006522998_751199848395776%2FLikes&version=v2.9
**In this link Right side Get Token indide GetAccessToken to select Permission**
303261006522998_751199848395776/Likes
303261006522998_751199848395776/Comments
{
"data": [
{
"id": "124778301449917",
"name": "Manisha Gera"
},
{
"id": "1680577265523548",
"name": "Rubi Sharma"
}
],
"paging": {
"cursors": {
"before": "MTI0Nzc4MzAxNDQ5OTE3",
"after": "MTY4MDU3NzI2NTUyMzU0OAZDZD"
}
}
}

How to expose a Django model as a RESTful web service?

I'm trying to create a REST web service that exposes the following Django model:
class Person(models.Model):
uid = models.AutoField(primary_key=True)
name = models.CharField(max_length=40)
latitude = models.CharField(max_length=20)
longitude = models.CharField(max_length=20)
speed = models.CharField(max_length=10)
date = models.DateTimeField(default=datetime.datetime.now)
def __unicode__(self):
return self.name
Here's how I thought about it so far:
Get all Persons
URL: http://localhost/api/persons/
Method: GET
Querystring:
startlat=
endlat=
startlng=
endlng=
Used for getting the Persons that are within the specified coordinate range.
page=
Used for getting the specified page of the response (if the response contains multiple pages).
Returns:
200 OK & JSON
404 Not Found
Example:
Request:
GET http://localhost/api/persons/?startlat=10&endlat=15&startlng=30&endlng=60
Response:
{
"persons":
[
{ "href": "1" },
{ "href": "2" },
{ "href": "3" },
...
{ "href": "100" }
],
"next": "http://localhost/api/persons/?startlat=10&endlat=15&startlng=30&endlng=60&page=2"
}
Get info on a specified Person
URL: http://localhost/api/persons/[id]
Method: GET
Returns:
200 OK & JSON
404 Not Found
Example:
Request:
http://localhost/api/persons/5/
Response:
{
"uid": "5",
"name": "John Smith",
"coordinates": {
"latitude":"14.43432",
"longitude":"56.4322"
},
"speed": "12.6",
"updated": "July 17, 2009, 8:46 a.m."
}
How correct is my attempt so far? Any suggestions are highly appreciated.
{ "href": "1" },
1 is hardly a valid URL. You should use full URLs. Google for HATEOAS.
Also, remember to send a relevant Content-Type header. You may want to make up your own mime-type to describe the format. This gives you the option to later change the content-type (Eg. change the format after publishing). See Versioning REST Web Services
I think query parameters could be simpler and clearer. This would make the URI more readable and would allow more flexibility for future extensions:
GET http://localhost/api/persons/?latitude=10:15&longitude=30:60
You may want to enable these in the future:
GET http://localhost/api/persons/?latitude=10&longitude=60&within=5km
Seems REST-cool. Even i worked on same kind of thing, few days earlier.
The only change, i would love to do in it, is the direct link to the person details. And also some details (like name here) to identify the person, and aid me in decision to navigate further. Like...
{
"persons":
[
{ "name": "John Smith", "href": "http://localhost/api/persons/1/" },
{ "name": "Mark Henry", "href": "http://localhost/api/persons/2/" },
{ "name": "Bruce Wayne", "href": "http://localhost/api/persons/3/" },
...
{ "name": "Karl Lewis", "href": "http://localhost/api/persons/100/" }
],
"next": "http://localhost/api/persons/?startlat=10&endlat=15&startlng=30&endlng=60&page=2"
}
This way, i am giving everything, to present data as,
John
Smith
Mark
Henry
Bruce
Wayne
...
Karl Lewis
Next Page
It's ok to provide shorthand URIs in your JSON responses if you provide some templating system. Like giving a base URI as something like http://whatever.com/persons/{id}/ and then providing IDs. Then with python you can just do a format call on the string. You don't ever want to make the programmer actually look at and understand the meaning of the URIs, which isn't necessary when you use templates.
You might want to take a look at pre-existing REST middleware. I know they saved me a lot of time. I'm using http://code.google.com/p/django-rest-interface/. And a snippet of the urls.py
json_achievement_resource = Collection(
queryset = Achievement.objects.all(),
permitted_methods = ('GET',),
responder = JSONResponder(paginate_by = 10)
)
urlpatterns += patterns('',
url(r'^api/ach(?:ievement)?/(.*?)/json$', json_achievement_resource),
)