I am new to Facebook PHP SDK for the Graph API.
I have created an application on Facebook and I got the application id, secret ..etc.
I also got the access code with some permissions using the scope parameter.
Using that code I got the access token with offline access.
https: //graph.facebook.com/oauth/access_token?client_id=xxxxxxxxxx&redirect_uri=http://192.168.15.5/xxxx/facebook/&client_secret=xxxxx&grant_type=client_credentials.
Using this I got the access token.
Now my Question is that I want to access the user information such as photos and friend lists
I do not know if it is correct or not,
https://graph.facebook.com/me/friends?access_token=xxxxxxxxxxxxxxxxxxxxx
when I use this it says,
"{
"error": {
"type": "OAuthException",
"message": "An active access token must be used to query information about the current user."
}
}"
How can I access the user information, by using the access token of that user.
I want to access the user information from the application at anytime for the user who has allowed my application to access his information.
If you use the Facebook PHP SDK (see on github), you should not call these URL directly but only call functions of the SDK.
See this answer to see how to fetch and use the offline access token to make API calls : How to login with offline_access using the new Facebook PHP SDK 3.0 ?
Then to get the user friends and their photos, you will call :
$args = array("fields" => "name,picture");
$friends = $facebook->api('/me/friends', $args);
Then the array $friends['data'] will contain the URLs of the pictures of the friends :
Array(
[0] => Array(
[name] => Quentin Pleplé
[id] => 1536397056
[picture] => http://profile.ak.fbcdn.net/...jpg
)
[1] => ...
...
)
Hope that helps !
Related
We want to develop a website on which users can publish photos on their behalf on their business Instagram account.
We have created a facebook application and we have done all the steps on
https://developers.facebook.com/docs/instagram-api/getting-started
to add user's Instagram account in our website.
In the mentioned link it is said that instagram_basic and pages_show_list permissions are needed so we have requested them and pages_show_list, pages_read_engagement, instagram_basic are approved and we switched our app to live mode.
When we test with a facebook developer account, we have problem with step 5 (Get the Page's Instagram Business Account GET /{page-id}?fields=instagram_business_account) in https://developers.facebook.com/docs/instagram-api/getting-started, we get this error:
{"error":{"message":"(#100) Object does not exist, cannot be loaded due to missing permission or reviewable feature, or does not support this operation. This endpoint requires the 'pages_read_engagement' permission or the 'Page Public Content Access' feature or the 'Page Public Metadata Access' feature. Refer to https://developers.facebook.com/.../login-permissions..., https://developers.facebook.com/docs/apps/review/feature... and https://developers.facebook.com/docs/apps/review/feature... for details.","type":"OAuthException","code":100,"fbtrace_id":"Aus-C-rSHZld-9rLWtVUJdC"}}
And when we test with a usual facebook account we have problem with step 4 (Get the User's Pages GET /{user-id}/accounts) in https://developers.facebook.com/docs/instagram-api/getting-started, we get empty response although the facebook account has different facebook pages.
I finally figured this out. The issue was I put Authorization : "Bearer " outside headers. If you put Authorization : "Bearer " inside headers, it should solve this problem. It will look something like this.
headers: {
"Content-Type": "application/json",
Authorization:
"Bearer ",
},
I am trying to post to my own facebook wall. So I created an "app" in my
personal facebook page, and got the app_id, app_secret, etc.
I then did this code:
#oauth = Koala::Facebook::OAuth.new(app_id, app_secret, callback_url)
#token = #oauth.get_app_access_token
#graph = Koala::Facebook::API.new(#token)
foo = #graph.get_object('me')
However, I get this error:
An active access token must be used to query information about
the current user. [HTTP 400]
(Koala::Facebook::AuthenticationError)
The token is valid, I checked. I need to post to my OWN wall, not a
different user's. From what I've read in the documentation, I need an "app
access key", not a "user access key" to do this. I am somewhat new to
the facebook api list, so I think I'm missing something very basic.
You can use an app token instead of a user token to post to a wall as long as the user already granted the proper permissions to your application.
That is, in timeline
User grants app access to post with publish_actions
User access token supplied by Graph Login Flow
At this point, you can either use the user access token or application access token
In addition,
foo = #graph.get_object('me')
is not a POST request. It says, get the object from the graph named me. Further me will not resolve to anything if you are using an application token because there is no way for the application to tell which "me" in all the users in the app you are referring to. Thus you need to refer to the app scoped id for the user.
e.g
foo = #graph.get_object('4')
Where 4 is a numerical app scoped ID (4 will not work in your case you need to figure out the correct ID for your application). The correct call in koala will be something like
foo = #graph.put_connections("4", "feed", :message => "I am writing on my wall!")
My problem was that the user (in this case myself) has to allow access to my app to post to my wall.
The full OAuth process is described well at http://developers.facebook.com/docs/authentication/
But specifically, I need to get a URL that I have to visit and then say "yes" to the authnetication question. The code is here:
#oauth = Koala::Facebook::OAuth.new(app_id, app_secret, callback_url)
#oauth.url_for_oauth_code(:permissions => "publish_actions")
The URL will look something like this:
https://www.facebook.com/dialog/oauth?
client_id={app_id}&
redirect_uri={redirect-uri}&scope=publish_actions
Note, the URL has to specify what permissions you want to request from the user (in this case, permission to post to the wall). This permission request is specified under the "scope" variable. Note some version of the facebook api allow posting through the "publish_stream" scope and other versions require the "publish_actions" scope. More information about the permissions available under scope variable is available here: https://developers.facebook.com/docs/facebook-login/permissions/v2.0
When you visit the URL that is generated from the above statement, facebook will give you a message asking if that particular app has permission to post to your wall. You, of course, say "yes". After that, your facebook app can post to the facebook wall using the "app access token"
After that, it's easy to post to the wall with your app access token. The code that works for me is:
#oauth = Koala::Facebook::OAuth.new(app_id, app_secret, callback_url)
#app_access_token = #oauth.get_app_access_token
#graph = Koala::Facebook::API.new(#app_access_token)
foo = #graph.put_connections(facebook_user_id, "feed", :message => "Test message")
I have a web service that servers Android, IOS and Windows Phone. Each app connects to Facebook using native SDK and passes the fb_user_id and access_token when performs API's requests. The API should retrieve the user and friends details and save them on MySQL.
The system works fine, except that it can't return friend's retionship, location and birthday.
$fb = new Facebook([
'appId' => my_app_id,
'secret' => my_app_secret
]);
$r = $fb->api("$fb_user_id/?fields=friends.fields(relationship,birthday,location)&access_token=$access_token");
var_dump( $r );
My app on Facebook has friends_relationships, friends_location and friends_birthday permissions. When we test using a Facebook user setted as developer, the app returns all the information, but when a non-developer uses we can't return those informations.
Any idea?
I figured what happened!
Facebook's SDK generates a token for /me. In PHP I was calling fb_user_id?fields=friends.fields(id,name,birthday,etc). But requests with /fb_user_id can't return info with a /me generated token.
It works when used directly on browser https://graph.facebook.com/me?fields=friends.fields(id,name,birthday,etc), but fails when called by Facebook's PHP class:
An active access token must be used to query information about the current user.
My solution:
$url = "https://graph.facebook.com/me?fields=id,name&access_token=$this->access_token";
$this->me = file_get_contents ( $url );
$json = json_decode($this->me, true);
For further information, checkout this answer. New ideas are welcome.
I am trying to pull some statuses in json format from Facebook's Graph API. Since i am using PHP, so I went for the following library.
https://github.com/facebook/facebook-php-sdk/blob/master/examples/example.php
I saw the example, and the class requires some arguments upon instantiation, and I just don't know what they are:
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => '344617158898614',
'secret' => '6dc8ac871858b34798bc2488200e503d',
));
And also from the Graph API Explorer I see the access_token, but this PHP library doesn't mention it at all, I thought I need the access_token to make requests, don't I? I hope someone can clear things up for me.
Yes you require an access_token to query on behalf of user on Facebook, it can either be an App access token or User access token. If the user authorizes your app, with required permission which in your case would be read_stream permission, then you can obtain his access token and query for status updates. The default access token is your App access token which can access only public data.
Also I would suggest you to to over the PHP SDK documentation for getting started with it.
I am creating a web application using php. I have a closed group in facebook. When a user login to my application I am showing them a page. I want to stream data grom my facebook group and display in that page. Since this is a closed group I am not able to stream data from that using php SDK. I am using the following code.
This code workes if the group is public.
$facebook = new Facebook(array('appId' => FB_APP_ID, 'secret' => FB_SECRET_KEY, 'cookie' => true,));
return $facebook->api('/' . FB_GROUP_ID . '/feed');
I think that we have to pass access token with extended permission to get that data. But how to generate that token without user intevention ?
Any Ideas?