fetch facebook user's friends with javascript - javascript

I am a new comer of StackOverFlow. As title, I 'd like to pick a subset of the user's friends, and I have tried that in the Graph API Explorer with → /v2.1/ me?fields=friends. However it return an empty json named data and the total count of the user's friends. What did I miss?? Below is my test in the facebook Graph API explorer.. ! Here comes the result in the graph api explorer.
{
"friends": {
"data": [
],
"summary": {
"total_count": 15
}
},
"id": "585768444863088"
}

You can also just use /me/friends instead, but the empty array is correct. You only get those friends who authorized your App too, you don´t get every friend anymore since end of April 2014 (v2.0 of the Graph API).
See here: https://developers.facebook.com/docs/apps/upgrading#upgrading_v2_0_user_ids
/me/friends returns the user's friends who are also using your app
There are ways to get the whole friendlist like invitable_friends and https://developers.facebook.com/docs/graph-api/reference/v2.1/user/taggable_friends, but those endpoints are supposed to get used for very specific tasks (for inviting in games and for tagging).

Related

How to get all google reviews using business api

I need all google reviews for particular location but I am unable to use business api from google. Here is url for get request
https://mybusiness.googleapis.com/v3/accounts/account_name/locations/location_name/reviews
Now my question is what is the value for param account_name and location_name
How can I get that.
Please answer with sample location example
I think first of all you need to white list your google my business api for whatever project you are working on in your project as its private api. Google my business api will work on the locations associated with your account so make sure you verified the LOCATIONS from any account you know. Then you can try out the api call you mentioned in OAuthplayground.
Follow steps mentioned in below documentation URL to set it up:
https://developers.google.com/my-business/content/prereqs
After the setup and etc you will automatically understand the account id and location id.
Also few more urls you can go to understand it better.
https://console.developers.google.com (here you will setup your project)
https://business.google.com/manage (here you will add/can see the locations - for which you need reviews)
https://developers.google.com/my-business/content/basic-setup (Steps after completing the prereq)
https://developers.google.com/oauthplayground (You will test the my
business api here after approval)
When you make a request to https://mybusiness.googleapis.com/v3/accounts it gives you a list of accounts. On those accounts they have a field called name. That field is accounts/account_name.
{
"state": {
"status": "UNVERIFIED"
},
"type": "PERSONAL",
"name": "accounts/1337",
"accountName": "example"
}
When you make a request to https://mybusiness.googleapis.com/v3/accounts/account_name/locations it gives you a list of locations. On those locations they have a field called name. That field is accounts/account_name/locations/location_name.
{
"locations": [
{
"languageCode": "en",
"openInfo": {
"status": "OPEN",
"canReopen": true
},
"name": "accounts/1337/locations/13161337",
...
}

Google Analytics for short urls

In javascript, I am trying to access analytics data for google shorten urls, for example. I tired 'URL Shortener API', which worked fine and I received data. But this data doesn't have analytics report for each hour in the day or for each day in the month, as its available on here. Here in response it have some properties for example 'clicks' and 'buckets' which contain the clicks count I need. Check the image below:
But these properties are not available in the data I received with the 'shortener API'. I might use Google analytics api for this purpose. Can anyone suggest me how can I use analytics api to get the analytics for any shorten url ?
Thanks
Are you sure you're using the URL Shortener API correctly ?
If I check the example you provided which contains the data you need like reports for the past two hours (per hour does not exists) or past day, I can see for example:
6 total clicks for the past two hours.
1243 clicks for the past day.
If I try to get the same data for the same short url with the URL Shortener API:
curl -X "GET" "https://www.googleapis.com/urlshortener/v1/url?shortUrl=http://goo. gl/fbsS&projection=FULL&key=YOUR-API-KEY"
I'll get the same data:
{
"kind": "urlshortener#url",
"id": "http://goo. gl/fbsS",
"longUrl": "http://www.google.com/",
"status": "OK",
"created": "2009-12-13T07:22:55.000+00:00",
"analytics": {
"allTime": /* ... */,
"month": /* ... */,
"day": {
"shortUrlClicks": "1243",
/* ... */,
},
"twoHours": {
"shortUrlClicks": "6",
/* ... */,
}
}
}
So I have 1243 clicks for the past day and 6 for the past two hours, the data are identical.
If you need to get all data from all time, you'll either have to store the data yourself or like you said use Google Analytics.
Google Analytics and short URLs can be pretty tricky to handle in Analytics because they redirect users from their website to your website which can cause Analytics to treat them as "direct" and not coming from any campaign you specified (newsletter, facebook, twitter, etc.).
You need to tag your URLs in order to properly track them. Usually, you'll need to use Google URL Builder to generate custom campaign parameters for your URLs.
There is no API for Google URL Builder but you can generate yourself valid URLs using the detailed informations provided on the previous link and append some or all of the parameters at the end of your non-short URLs like utm_source, utm_medium, utm_term, etc.
When your non-short URLs are properly tagged, you can then shorten them using any service you want.
To get the data back, you'll need to use the Google Analytics API and specifically the Reporting API.
Once authenticated,
var discoveryURL = 'https://analyticsreporting.googleapis.com/$discovery/rest?version=v4';
// Load the API
gapi.client.load(discoveryURL)
.then(function() {
// Returns Analytics data.
gapi.client.analyticsreporting.reports.batchGet({
"reportRequests": [
{
"viewId": VIEW_ID,
// View IDs can be fetched from the Analytics Account Explorer
// https://ga-dev-tools.appspot.com/account-explorer/
"dateRanges": [
{
"startDate": "7daysAgo",
"endDate": "today"
}
],
"metrics": [
{
"expression": "ga:sessions"
}
]
}
]
})
.then(function(response) {
var json = JSON.stringify(response.result, null, 2);
// Do anything you want with the JSON returned.
});
});
The main function used here is batchGet and you can get every informations regarding the fields and options you can use on the Reporting API v4 reference.
You'll be able to toy with various fields like dates (DateRange), dimensions, etc. to get all the data you'll need in your application.

How to get posts that user's friend posted on the wall of user on his birthday through Facebook Graph Api?

I am trying to build a Chrome extension and I want to get all the posts that people posted on the user's wall specifically on his birthday.
I tried to call through Facebook Javascript SDK, but it is not returning the posts that are posted by my friends on the birthday.
FB.api(
'/me/feed',
'GET',
{},
function(response) {
// Insert your code here
}
);
Rather, it is returning all the birthday posts in one post like this:
{
"message": "Belated happy birthday bhaiii.....",
"created_time": "2015-10-19T00:02:02+0000",
"id": "578828115566706_848912165224965"
},
{
"story": "Ritwik Jain and 40 others wrote on your Timeline.",
"created_time": "2015-10-18T03:59:39+0000",
"id": "578828115566706_848604468589068"
},
How do I get each post individually?
Thanks in advance.
As you found out, the /me/feed endpoint is what you need to use - with the user_posts permission, of course. Facebook puts birthday posts together as it seems, but there is no other way to get the posts. Meaning, there is no way to get the birthday posts separately.
Endpoints for the user wall: https://developers.facebook.com/docs/graph-api/reference/v2.5/user/feed

Determine weather user liked a photo or not. Facebook Graph API v2.2

I am working on Facebook application (JS sdk), In this app user can post like/comment on a photo. For this activity, It is showing a like button at bottom of photo. But if user already liked particular photo then we don't show like button there. For this we need to know weather user already liked the photo or not. It is possible to know this using FQL
SELECT object_id,like_info,comment_info FROM photo WHERE object_id=343907262480651
And it returns a boolean flag data["like_info"]["user_likes"]
I am migrating my code from graph api v1.0 to v2.2. So FQL will is longer available.
I want solution in graph API 2.2 similar to user_likes flag. I searched a lot for this, but not getting what exactly i want. Is there any alternative way to achieve this?
Thanks.
To get the likes for an object, you make call to
/{object-id}/likes
If you want the total number as a field, you need to set the field summary=true
I'm trying to accomplish the exact thing. So far, I couldn't find any end point that will readily tell you whether the user has already liked the post or not. But one work around is to call the /{object-id}/likes end point that will return you something like below:
{
"data": [
{
"id": "808004209267865",
"name": "Kailey Nicole"
},
{
"id": "10204107012455593",
"name": "Brenda S. Coffelt"
}
],
"paging": {
"cursors": {
"before": "OTU1NzQ4ODQxMTIyNDEz",
"after": "MTAyMDQxMDcwMTI0NTU1OTM="
},
"next": "https://graph.facebook.com/v2.3/10151929326420059/likes?pretty=0&limit=25&after=MTAyMDQxMDcwMTI0NTU1OTM="
}
}
You can then easily look for the user id in this JSON array. If it exists that means the user likes the page otherwise not. However keep in mind that there can be pagination (in case of lot of likes), so you'd really have to keep using the next URL to exhaust all likes data.

Getting like and comment counts per post of facebook page by graph api version 2

I have been getting like and comment counts per post of facebook page/group feed call by graph api separately using FQL but as version 2 of graph api released fql no longer working to serve purpose.
So i have to find new ways to get comment and like counts per post of page feed display. I will make a separate call to get comment and like counts per post of the fb page as it may not be possible to get things in same page feed call(or it is?).
So, searching through google, i found following way using graph api call -
..page_id/feed?fields=likes.limit(1).summary(true){id},comments.limit(1).summary(true)&limit=10
Is this the best and error free way?? Also besides id and summary fields i also get created_time, paging, likes data by the above call which is unexpected and redundant, how do i exclude these additional fields?
So please any FB employee show me light on what is the best way to retrieve like and comment count per post of page/group feed using graph api version 2.
If you want to retrieve Likes and comments count of a post on FB you can achieve this by using Id of the post Like this
..Your_Post_ID?fields=likes.limit(0).summary(true),comments.limit(0).summary(true)
the result will contain
Total numbers of likes of the post, total numbers of comments of the post, post ID and post created time.
The result will be like this
{
"likes": {
"data": [
],
"summary": {
"total_count": 550
}
},
"comments": {
"data": [
],
"summary": {
"order": "chronological",
"total_count": 858
}
},
"created_time": "2014-10-12T05:38:48+0000",
"id": "Your_Post_ID"
}

Categories